The command

nohup python your-script.py &
  • nohup: This stands for “no hang up.” It’s a Unix command that is used to run another command (in this case, python your-script.py) in a way that it continues to run even after you close the terminal or log out of your session. Essentially, it prevents the command from being terminated when you log out.
  • &: The ampersand at the end of the command tells the shell to run the preceding command (in this case, the Python script) in the background. This means that your script will run independently in the background, allowing you to continue using the terminal for other tasks without waiting for the script to finish.

When you execute this command, it will start your Python script in the background, and you’ll see a message similar to

nohup: ignoring input and appending output to 'nohup.out'

This means that any output generated by your script will be written to a file named nohup.out in the same directory where you ran the command. So, if you wish to view a snapshot of the file as it exists at that moment, run:

nano nohup.out

Why the ampersand?

The main difference is that the script will run in the foreground, which means it will take control of the terminal; you won’t be able to use the terminal for other tasks until the script finishes executing, which can be inconvenient if the script takes a long time to complete.

However, the script will still be executed using nohup, meaning the process will still run when you log out or close the terminal.

Why do I need this command?

This command allows you to run scripts or programs in the background. This means that, if you’re connected to a remote server, you can use nohup and then disconnect from the server, without having to actively be on the server to maintain the active running of the script.

This also means that, if you’re using a remote server, you can completely shut off your computer, go get lunch, comeback, and login to the remote server to see that you’re script is still running–as if nothing happened!