Python Virtualenv – Run Background Job – Linux

Python – Using Python with Virtualenv, allows the creation of easy containment of all the dependencies without having them all installed on the main system. However, creating a background process within this environment is active, and later terminating the terminal window will terminate the background process. This makes it very inconvenient when running, for instance, a machine learning model training in a background inside a terminal. When that window closes, so does the program.

One Solution is to use the nohup program (no hang-up). It disables the SIGHUP signal to travel down to the Python program. Preventing it from terminating the program when closing the window/terminal.

The following will run the Python script as a background process. However, it should be noted that by default it creates a nohup.out where it redirects the stdout.

nohup python script.py &