Image from Wikimedia Commons

Running a Python Script in the Background

Table of Contents

This is a quick little guide on how to run a Python script in the background in Linux.

Make Python Script Executable

First, you need to add a shebang line in the Python script which looks like the following:

#!/usr/bin/env python3

This path is necessary if you have multiple versions of Python installed and /usr/bin/env will ensure that the first Python interpreter in your $PATH environment variable is taken. You can also hardcode the path of your Python interpreter (e.g. #!/usr/bin/python3), but this is not flexible and not portable on other machines. Next, you’ll need to set the permissions of the file to allow execution:

chmod +x

Start Python Script in Background

Now you can run the script with nohup which ignores the hangup signal. This means that you can close the terminal without stopping the execution. Also, don’t forget to add & so the script runs in the background:

nohup /path/to/ &

If you did not add a shebang to the file you can instead run the script with this command:

nohup python /path/to/ &

The output will be saved in the nohup.out file, unless you specify the output file like here:

nohup /path/to/ > output.log &
nohup python /path/to/ > output.log &

Find and Kill the Running Process

You can find the process and its process Id with this command:

ps ax | grep

If you want to stop the execution, you can kill it with the kill command:

kill PID

It is also possible to kill the process by using pkill, but make sure you check if there is not a different script running with the same name:

pkill -f

Output Buffering

If you check the output file nohup.out during execution you might notice that the outputs are not written into this file until the execution is finished. This happens because of output buffering. If you add the -u flag you can avoid output buffering like this:

nohup python -u ./ &

Or by specifying a log file:

nohup python -u ./ > output.log &