Bash script should check if a certain process is running more than a certain number of minutes, and kill it if does.
I can get the running time by something like
ps -aux | grep ProgramName | grep -v grep | awk '{print $10}'
That gives 9:47.31 for instance. But where do I go further and check if that is greater than, say 10 minutes threshold?
Here is the awk 1 liner you’ll need for your use case:
Also note that
ps -o etime -C ProgramNamegives you the time since ProgramName has been running so you don’t need to use your overly complicated command to get this time.IMPORTANT: Also remember that for the processes that have been running for more than a day you will get output of
pscommand as something like1-21:48:48. I don’t have this case covered in my awk command but you can use the sameawk's splitcommand as I have shown above.UPDATE: As per the comment below, use this version for FreeBSD or any other flavor of Unix (eg: Mac) where
-C ProgramNameoption is not available.