-1

on the Linux (CentOS 6), i want to kill process containing "pkgacc" in its command (so no exact command but just partial match) if it is running more than 36 hours.

There is related question: How do you kill all Linux processes that are older than a certain age? but any of the solutions provided do not work for me.

when executing:

 if [[ "$(uname)" = "Linux" ]];then killall --older-than 1h someprocessname;fi

It just return killall usage page on how to use killall, in its manual page there is no mention about "--older-than" switch.

16851556
  • 255
  • 3
  • 11

2 Answers2

1

It is infinitely easier to invoke a program in a wrapper like timeout from GNU coreutils than to go hunting for them after the fact. In particular because timeout owns its process, there is no ambiguity that it kills the right process. Thus

timeout 36h pkgaccess --pkg_option --another_option package_name

where I made up the names and options for the pkgaccess command since you didn't give them. This process will run no longer than 36 hours.

msw
  • 42,753
  • 9
  • 87
  • 112
0

I think you could do something like

ps -eo pid,cmd,etime

then you could parse the output with grep searching for you process, something like that:

ps -eo pid,cmd,etime | grep pkgacc

you will have some output with one or more result, the last column from the output must be the time of running process, so one more little bash effort and you could check if the time is greater than 36 hours.

#!/bin/bash

FOO=$(ps -eo pid,cmd,etime | grep -m 1 pkgacc | awk '{ print $1" "$3 }'| sed -e 's/\://g')
IFS=' ' read -r -a array <<< "$FOO"
if [ "${array[1]}" -gt "360000" ]; then
    echo "kill the process: ${array[0]}"
else 
    echo "process was not found or time less than 36 hours"
fi

I think that could solve part of your problem, see that I do not explicit kill the process but just indicate what it is. You could improve the idea from that.