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: 🔍🛑

  1. Open the terminal.

  2. Use the ps command to find the processes related to your Python script. For example, if your script is named my_script.py, run:

    ps aux | grep my_script.py
    

    This will display a list of processes related to my_script.py.

  3. 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.py
    

    In this example, the PID is 12345.

  4. Use the kill command to terminate the process:

    kill 12345
    

    Make sure to replace 12345 with 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