Use python to record the running pid, and kill their instances as needed. pythonpid

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.