Managing Processes in Bash on Linux
To send a running process to the background, you first need to pause it:
Ctrl-z
Get the number of the paused job:
jobs -l
then send it to the background with the bg command:
bg %jobnum
You can bring a process back to the foreground with the fg command:
fg %jobnum
To keep a process from being interrupted when the terminal session ends, you need to run it inside screen. But what do you do if the process has already been running for a few hours with no end in sight? Killing it and losing all that time feels like a waste.
Don’t lose heart - you can send the process to the background using what’s described above, then cut all ties with it:
disown -h %jobnum
Now you can safely close the terminal window, the running command will keep going without you.
You can check whether a job is still running with the ps utility:
ps -aux |grep %process_name%
The second column is the process’s PID.
You can also use the kill utility to control processes:
- kill -9 PID - stops execution
- kill -20 PID - pauses (suspends) execution
- kill -18 PID - resumes a paused process.