In this lesson we will learn how to manage processes in Linux. We will learn how to run processes in the background, how to pause and resume them, and how to run them in the foreground again.
To begin the exercise, ensure you have python3 installed and download the mock script that mimics a robot driver program.
wget https://raw.githubusercontent.com/nevalsar/mrsd-practice-git/main/autonomous-robot-mock.py -O robot-driver.pyYou also need to ensure that the script is executable by setting executable permissions.
chmod +x robot-driver.pyTest that you're able to execute the script by running it.
./robot-driver.py 5-
Run the
sleepcommand in the background by appending&at the end of the command./robot-driver.py 10 &You will notice that the terminal is not blocked and you can run other commands.
-
List the jobs running in the background to verify
jobs
-
Run a command in the foreground
./robot-driver.py 10
You will notice that the terminal is blocked by the process running in foreground and you cannot run other commands.
-
Move the foreground command to background by pressing
Ctrl + ZYou will notice that the terminal is not blocked anymore and you can run other commands.
-
List the jobs running in the background to verify
jobs
-
Run a command in the background by appending
&at the end of the command./robot-driver.py 10 & -
Move the process to foreground using
fg:fgYou will notice that now the terminal is blocked by the process running in foreground and you cannot run other commands.
-
Run the
sleepcommand in the background by appending&at the end of the command./robot-driver.py 10 & -
Get the PID of the
robot-driver.pyprogramjobs -lYou will notice that the PID of the running program is listed against its name. Note it down.
-
Pause the command by sending it the
SIGSTOPsignalkill -STOP <pid>
-
Resume the
sleepcommand by sending it theSIGCONTsignalkill -CONT <pid>