Operating System - HP-UX
1824807 Members
4010 Online
109674 Solutions
New Discussion юеВ

Kill process PID using cron

 
SOLVED
Go to solution
Abhijeet_3
Frequent Advisor

Kill process PID using cron

On my server , perl process is utilising high cpu and memory.

I need to kill this perl process at a predefine time using cron.

I am able to find PID using
ps -ef | grep perl | awk '{ print $2 ; } '

how to use this value , to kill process.

What will be the correct syntax of script.

Abhijeet
4 REPLIES 4
Alex Lavrov.
Honored Contributor

Re: Kill process PID using cron

ps -ef | grep perl | awk '{ print $2 ; } '
| xargs kill -9


"xrags" command takes all the stdin and sends it as arguments to the command, in this case "kill -9".
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Biswajit Tripathy
Honored Contributor
Solution

Re: Kill process PID using cron

You might want to modify it a little as this:

ps -ef | grep perl | grep -v grep | awk '{print $2}' | xargs kill -9

The middle "grep -v grep" is to make sure that your
"grep perl" process does not show up in the input to
perl.

- Biswajit
:-)
Taulant Shamo
Frequent Advisor

Re: Kill process PID using cron

Hi Abhijeet,

is better to create e file called killperlpid.sh which will be executed to Kill the process you need.

#cat killperlpid.sh
!/usr/bin/sh
kill -9 `ps -ef | grep perl | awk '{print $2}'`

Put it in crontab like below:
you can choose how ofeten do you want to run the script.
For example if you want it to be excecuted every 10 minutes.

00 10 20 30 40 50 * * * * /killperlpid.sh 2>&1


*****
1-st star is for minutes *=every min
2-nd star is for hours *=every hour
3-rd star is for days *=every day or specific day 0,1,2,3,4,5,6 From Sunday - Saturday
4-th star is for month *=every month or specific month 1,2, ... , 12
5-th star is for year *=every year or for specific



Taulant
Abhijeet_3
Frequent Advisor

Re: Kill process PID using cron

Thx everyone.