Ctrl-Z + bg
I used to use the trick of hitting Ctrl-Z when a long running process in running in the terminal and then entering bg to background it.
nohup
But, I have a new favorite way to background using nohup. Lets say I am running a program like one that calculates pi called pi, for example. I can enter…
nohup pi &
…and it keeps the process running even if I kill the parent process, as inĀ the shell that I launched it from, by closing it. All of the output from the program winds up in the directory in a file called nohup.out.
Stopping the backgrounded program
If I really want to stop the program, I can do it via kill.
First find the process ID (PID) using…
ps aux | grep pi
…what I get is this…
erick 2315 89.5 0.6 10884 8116 pts/1 R+ 18:24 0:10 pi 10000000
The PID is the first number, in this case 2315. So I will use…
kill 2315
…and this process will be killed gracefully by sending a SIGTERM to ask the process to stop, just like a Ctrl-C would do from the command prompt.
Forcing a backgrounded program to stop
If a process won’t stop when you issue a plain kill (SIGTERM) you an send a sigspec, specifying the type of kill set to KILL to force it to quit…
kill -s KILL 2315
…will shut the program down immediately without saving to disc or whatever the program might do to clean up ( might not free up memory that was MALLOC’d for instance ) when it shuts down.
You can see the difference if you run a process in one terminal and use kill in another.
kill 2315 gives you…
erick@erick-laptop:~/pi$ pi 100000000 Terminated
where
kill -s KILL 2315 gives you…
erick@erick-laptop:~/pi$ pi 100000000 Killed