nohup
The command nohup
(stands for “No hang up”) allows your script to run even if you quit the terminal. It can be very useful, especially if your terminal has been opened through ssh
and you have a dodgy connection. It can be used as follows:
nohup python my_script.py > log.out &
nohup will automatically append the output from your script to a file named nohup.out
. By adding the > log.out
part of the command you can save the output to a different file of your choice.
As nohup
keeps processes running even after you have closed the terminal, it makes it hard to kill the process once it has started. This can easily be solved using ps
.
ps
The command ps
(stands for “process status”), reports information on current running processes, outputting to standard output. It can be used with the grep
command to find the ID of processes started with nohup
by searching for the name of your script.
ps -Af | grep my_script.py
Once you have the ID of the process you want to stop you can use the kill
command.
kill 123456
tail
The tail
command shows the final few lines of a text file. It has a special command line option -f
(follow) that allows a file to be monitored. This can be handy when used in combination with nohup
. If you have a process that you want to stay running when you quit the terminal, but you are also interested in its output, you can use the following command.
nohup python -u my_script.py > log.out & tail -f log.out
If you have got this far, I hope this was not a complete waste of your time.