How to execute a Python script using nohup in Linux?

To run a Python script in the background and not be affected by terminal closure on Linux, you can use the nohup command. Here are the steps to execute a Python script using nohup:

  1. Open the terminal.
  2. Switch to the directory where the Python script is located using the cd command.
  3. Execute the Python script using the following command:
  4. Run your_script.py in the background using nohup and Python.
  5. The file name “your_script.py” here refers to the Python script that you want to execute.
  6. After executing the command, a file named nohup.out will be generated to store the output content of the script.
  7. When the terminal is closed, the script will continue to run in the background.

You can also specify the name of the output file by using the following command:

nohup python your_script.py > output.log &

The “output.log” here is the name of the output file you need to specify.

To stop a Python script running in the background, you can use the following command:

ps -ef | grep your_script.py

Then find the corresponding process ID (PID) and use the kill command to stop the process.

kill PID

Please replace “your_script.py” with the actual file name of your Python script in the command above.

bannerAds