1825804 Members
2520 Online
109687 Solutions
New Discussion

Re: System process

 
peterchu
Super Advisor

System process

We have a system user "dbauser" always run some process in the system , but sometimes , these processes will not stop automatically l we need to terminate the process manually , can suggest the method how to terminate the process that is run by "dbauser" and run/idle over 10 minutes ? thx
11 REPLIES 11
generic_1
Respected Contributor

Re: System process

setup a cron job that does a ps -ef | grep dbauser. and look at the time the process has been running. Have the cron run every ten minutse to check for new ones. If your script finds a match, have it kill it.
peterchu
Super Advisor

Re: System process

thx reply ,

I am new to write script , could provide me some help ? thx
twang
Honored Contributor

Re: System process

I would suggest that you should take a look at why these processes keep running. How about the state of these processes, were they hung or in running state?
peterchu
Super Advisor

Re: System process

thx reply ,

all these are dead processes , the system run it normally but can kill the process automatically , could suggest a script ? thx
Pedro Tapia_2
Occasional Contributor

Re: System process

$ ps -fea |grep dbauser
Vibhor Kumar Agarwal
Esteemed Contributor

Re: System process

kill -9 `ps -aef | grep dbuser | awk ' { print $2 }'
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: System process

You can terminate them as,

ps -u | awk '{ split($3,a,":"); if ( a[2] > 10 ) { print $1; }}' | xargs kill -9

or

kill -9 `ps -u dbauser | | awk '{ split($3,a,":"); if ( a[2] > 10 ) { print $1; }}'`

hth.
Easy to suggest when don't know about the problem!
hangyu
Regular Advisor

Re: System process

thx Muth,
when run your script , it pop
sage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]

it seems my system can't use "split" , could provide some help again ? thx in advance
Ralph Grothe
Honored Contributor

Re: System process

It's quite easy to script a reaper for the long running process.
But as someone else already replied you should better check what's causing this.

Anyway you can parse the PID (to send a signal to), and the elapsed time of the process).
In my example I send a "sleep 600" in the background, and I intend to shoot it if it was running for more than 3 mins.

1. parse pid and etime and store in vars

UNIX95= ps -o pid= -o etime= -C sleep|read pid etime

2. test if etime > 3 min. and shoot

[[ $(printf "%d" $(echo $etime|cut -d: -f1)) -ge 3 ]] && kill $pid


Ideally you would put the test (i.e. the [[ ]]) in a loop conditional block.

e.g.

until [[ $(printf "%d" $(echo $etime|cut -d: -f1)) -ge 3 ]];do sleep 10;done; kill $pid


There are hundred similar ways to implement this little functionality (probably better ones).

However, some caveat.
If the process you want to kill already has been running for several hours the etime format changes to HH:MM:SS
So your parsing would have to accomodate for such subtle nuissances.

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: System process

Rubbish, please forget the until loop example!
I was too quick clicking forgetting thinking.
You of course need to put the variable assignment of $etime within the loop's body to update the value.
Madness, thy name is system administration
hangyu
Regular Advisor

Re: System process

thx Ralph Grothe 's working,

I am not too understand the statement "UNIX95= ps -o pid= -o etime= -C sleep|read pid etime" , it seems no output to the system , could suggest simipler way ? thx in advance.