1846140 Members
4558 Online
110254 Solutions
New Discussion

check process

 
SOLVED
Go to solution
hangyu
Regular Advisor

check process

i have a script that read a file which contains process_id and time that he's
in and it lookes like this
0:30 54545
0:44 66788
0:50 23233
i need to read every line in the file and get the time and if the process is greater then 0:30 to kill the process id
the script looks like this
cat bbb | while read line
do
#kill -9 $i
done
i don't how to take of the hour , ask about it and kill the process
can you please help me and another question
did i do the loop ok did i need the pipe before the while
14 REPLIES 14
Vibhor Kumar Agarwal
Esteemed Contributor
Solution

Re: check process

I think you got the most of it.

cat bbb | while read line
do
awk '{ if ($1<30) print kill -9 $2 }' | sh
done
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: check process

Use this:

while read time pid
do

sec=$(echo $time | cut -d":" -f1)

[[ $sec -gt 30 ]] && kill -9 $pid

done <

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: check process

Use simply as,

# awk '{ split($1,a,":"); if ( a[2] > 30 ) { print "kill -9 " $2 } }' | ksh

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: check process

With perl scripting, you don't need reverse shell execution as,

perl -ne '($time,$pid)=split; @min=split(/:/,$time); kill 9,$pid if $min[1] > 30;'

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

Re: check process

thx Muthukumar ,

I use your script , but I tried to echo the $sec , all sec are "0" ( it should be 30 , 44 , 50 ) , so no one record greater than 30 , could suggest what is wrong ? thx


while read time pid
do

sec=$(echo $time | cut -d":" -f1)

[[ $sec -gt 30 ]] && kill -9 $pid

done <

hth.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: check process

I think it will be -f2
Vibhor Kumar Agarwal
hangyu
Regular Advisor

Re: check process

thx replies ,

it works now , it I want to script to check the process 3 times in very 1 minutes , the crontab job only can run 1 time in 1 minute , could suggest what can I do ? thx
hangyu
Regular Advisor

Re: check process

or if I want the job run repeatedly , could suggest what can I do ? thx
Orhan Biyiklioglu
Respected Contributor

Re: check process

while true
do

while read time pid
do

sec=$(echo $time | cut -d":" -f2)

[[ $sec -gt 30 ]] && kill -9 $pid

done <

sleep 20

done

this will run in an infinite loop until you interrupt it.
hangyu
Regular Advisor

Re: check process

thx reply ,

the process can run repeatedly now , but if I add it to the cron , the process will run many many times , so I think it can't be add to the crontab , could suggest if not add to the crontab , how to make sure the job is keep on running ? thx.
Muthukumar_5
Honored Contributor

Re: check process

You can automate this script execution with crontab, at (schedulars) or with scripting running infintite.

#!/bin/ksh
# psck.ksh
while [ TRUE ]
while read time pid
do

ps -ef | grep -q $pid |grep -v grep
if [[ $? -eq 0 ]]
then
sec=$(echo $time | cut -d":" -f1)
[[ $sec -gt 30 ]] && kill -9 $pid
echo "$pid is killed @ $(date)" >>/tmp/logfile
fi

done <

sleep 60
done
# end

Run this script as,

# chmod +x psck.ksh
# ./psck.ksh &

# tail -f /tmp/logfile give details of process which killed.

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

Re: check process


your explaination is very detail , but I am not too understand how to make sure the script is running in the system if not add it to the crontab ? thx
Vibhor Kumar Agarwal
Esteemed Contributor

Re: check process

You require a script to run 3 times every minute, Righto

So make a loop in that script which runs 3 times.
Now add that script in cron scheduled for every minute.
Vibhor Kumar Agarwal
Bill Hassell
Honored Contributor

Re: check process

As mentioned, cron is limited to one minute resolution. So inside your script, either create a function that performs the kill and then sleep 20 (20 seconds) and repeat two more times, or just code the kill test 3 times with a sleep between each section.

By the way: are you sure you want to use kill -9? It is a very bad command to use against programs, especially databases and other applications that use shared application resources. It is strongly recommended that you use kill -15, followed by a kill -1 (if kill -15 doesn't work) and finally kill -9 as a last resort.


Bill Hassell, sysadmin