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:
- Open the terminal.
- Switch to the directory where the Python script is located using the cd command.
- Execute the Python script using the following command:
- Run your_script.py in the background using nohup and Python.
- The file name “your_script.py” here refers to the Python script that you want to execute.
- After executing the command, a file named nohup.out will be generated to store the output content of the script.
- 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.