Record using nohup command to execute py script
The nohup command allows you to run a program in the background, even if you close the terminal.
Run the following command in the terminal:
nohup python3 my_script.py &
To stop a Python script running in the background using the nohup command, you need to find the process ID (PID) of the script and then use the kill command to terminate the process. Here are the steps: 🔍🛑
Open the terminal.
Use the
pscommand to find the processes related to your Python script. For example, if your script is namedmy_script.py, run:ps aux | grep my_script.pyThis will display a list of processes related to
my_script.py.In the output, find the process ID (PID) related to your script. The PID is usually the second column in the output. For example:
user 12345 0.1 0.2 123456 7890 pts/1 S 10:00 0:01 python3 my_script.pyIn this example, the PID is
12345.Use the
killcommand to terminate the process:kill 12345Make sure to replace
12345with the actual PID.
Now your Python script should have stopped running. If you need to start the script again, simply run it using the nohup command. 🔁🚀
View process details
ps aux