How to terminate-stop-end the running of a Python program in the SSH2 terminal of Centos?

python3 /opt/python_scripts/retr/P-0006.py &

After I ran the above command, I got the following prompt:
[1] 42335
If I want to terminate this Python program I wrote myself, how should I do it?

A:
When you run a command in the background, the terminal will display [1] 42335a prompt like , where 42335is the PID (process ID) of the process. To terminate a Python program running in the background, you can use one of the following methods:

  1. Use killthe command: use killthe command to send a termination signal to the specified process. In this case, you can use the PID of the process to terminate the Python program. For example:

    kill 42335
    

    This will send a kill signal to the process with PID 42335, thus stopping the Python program from running.

  2. Using pkillcommands: pkillCommands allow you to kill processes based on their name. For example, if your Python program name is P-0006.py, you can use the following command:

    pkill -f P-0006.py
    

    This will kill P-0006.pyall processes related to .

Note that using these commands simply terminates the running program without any cleanup. If your program needs to perform some cleanup work (such as closing connections, saving data, etc.), it is best to catch the termination signal in the program and handle it accordingly.

psYou can also use the command to find running processes before terminating the program . For example, the following command can be used to list all Python-related processes:

ps aux | grep python

You can then find the PID of your Python program and use one of the methods above to terminate it.

I use the above pkillcommand, the effect is as follows:
insert image description here
Then I access the web service opened by the above code and find that the service has terminated.

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/131004677