I just finished a web scraping app made with puppeteer and node, and at this point in time it is very unstable due to relatively low to none experience with puppeteer (further debugs on it crashing is coming up). Sometimes, when running multiple instances of that app, one of them crashes and that node process won't terminate and it will stay up forever or until I manually kill it.
I am using: ps -e | grep node
to detect all node processes and: pkill -f node
to kill every process. Digging a bit in, I discovered this: ps -eo pid,comm,lstart,etime,time,args
, that returns every Linux process. The time
parameter returns the elapsed time of the process. Now my question is : is there a way to kill only those node processes of which the elapsed time is greater than 5 minutes?
A normal run of the puppeteer script usually does its job in around 3-4 minutes, depending on the amount of information it has to scrape.
Please please help! Thank you!
So ... building up on your ps
invocation:
ps -eo pid,comm,lstart,etime,time,args | awk '$10~/node/{split($8,a,/[-:]/);min=a[length(a)-1];if(min>=5){print $1}}' |xargs -i echo "kill "{}
You can use awk to do some post-processing.
If the 10th field contains the word node, split the etime
into an array a
based on the separators [-:]
; the second to last element will always be the minutes. If the 2nd to last element is >=5, print the respective PID. Handle awk's output with xargs ... I'm echoing "kill" and the pid because I didn't want to kill random processes in my testing.
If the test output looks sane, change xargs -i echo "kill "{}
to xargs -i kill {}