Killing Long Running User Processes in Linux using shell script

ContextMiner, one project what I am working on, gathers social media data that includes Twitter, YouTube, Flickers, and Blogs, in response to user’s keywords of interest. Depending on the setting that a user creates, the system accesses the corresponding social media to collect relevant tweets, video clips, pictures, and blog posts every day, week, or month.

To understand and monitor a particular video clip’s trend and popularity, as a time series format, the YouTube module retrieves the number of views and the number of likes of each video in the list of collected clips while the list is also updated every day/week/month adding more items. One problem here is the increasing number of instances that need to be updated along with the increasing list items. We’ve seen some processes hung for more than one day, possibly due to the burden related to MySQL access, while those processes have done the collecting process successfully. We needed a way in which the long running process can be detected and killed.

I found a couple of useful resources from stackoverflow about: (1) Shell program that reads elapsed time regarding long running processes, and (2) how to parse the elapsed time into seconds.

Referring those resources, I wrote a script that kills processes running more that one hour:

users=("user1" "user2")
for user in "${users[@]}"
 ps -o etime,euid,pid,tty,comm -u "$user" | while read etime euid pid tty comm
 [ "$etime" = ELAPSED ] && continue
 [ "$comm" != "php" ] && continue 
 E_SECONDS=$(echo $E_TIME | tr '-' ':' | awk -F: '{ total=0; m=1; } { for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}')
 #echo $E_SECONDS
 if [ $E_SECONDS -gt 3600 ]
 then kill $CUR_PID
 #then echo $CUR_PID $E_SECONDS $comm


Since we are running php command, I specified here only php, which can be edited for different commands/programs.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s