Use python to record the running pid, and kill their instances as needed. pythonpid
When I am running a crawler program, because the crawler program waits for a long time for the target server to return data, and the cpu usage is very low, I often run hundreds of them as soon as a proxy is running. However, crawlers usually write an endless loop, or exit until all tasks assigned to the process are completed. If we want to end these tasks halfway, we cannot manually kill them one by one. How can we end these processes?
My method is to record the Pid of The crawler process when running it, and then kill it with a statement when you need to manually end it.
For I in {1 .. 100} do nohup python NetEase_comms_proxy.py> hehe. dat 2> & 1 & echo $! > Run. pid sleep 10 done
The above statement completes the script to run 100 in the background, and append the pid to the run. pid file. Nohup is the abbreviation of no hang up. Do... Done is a circular statement.
The next step is to kill their statements. I use python
Import osif _ name _ = '_ main _': fin = open ('Run. pid ', 'R') pids = [] for line in fin: pids. append (line. strip () for pid in pids: cmd = 'Kill '+ pid OS. system (cmd)
In this way, all the recorded PIDs are killed.
Note that some processes exit when the task is running because the task is completed or an error occurs. Other programs may share the Pid of these processes, so that if you run the kill script, these processes may also be killed, causing unnecessary trouble.
The above example uses the python record to run the pid and kill them as needed. This is all the content that I have shared with you. I hope to give you a reference, we also hope that you can support the customer's home.