LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to kill the process after specific time? (https://www.linuxquestions.org/questions/linux-general-1/how-to-kill-the-process-after-specific-time-624453/)

samengr 02-28-2008 06:39 AM

how to kill the process after specific time?
 
Hi All,

I need to define the specific duration to run a process. i mean after specific time the process should be killed autometically.

plese help me to do this.

Thanks.

marozsas 02-28-2008 07:49 AM

You need to develop a bit more your question.
In what language you plan to write your application, the process you mentioned ?
Or is a regular application in binary format you want to control it ?
It is acceptable is this application receives a TERM signal to die or it need to follow some special procedure before that ?

Anyway, the simple case is to use kill to send a TERM signal to the application.

Code:

#!/bin/bash

# the application you want to terminate after a while
prog=xclock

# the time you want to wait before killing it
time=5m

# run it in background
${prog} &

# wait...
sleep $time

# ...and kill
kill %1


samengr 02-28-2008 07:59 AM

Quote:

Originally Posted by marozsas (Post 3072592)
You need to develop a bit more your question.
In what language you plan to write your application, the process you mentioned ?
Or is a regular application in binary format you want to control it ?
It is acceptable is this application receives a TERM signal to die or it need to follow some special procedure before that ?

Anyway, the simple case is to use kill to send a TERM signal to the application.

Code:

#!/bin/bash

# the application you want to terminate after a while
prog=xclock

# the time you want to wait before killing it
time=5m

# run it in background
${prog} &

# wait...
sleep $time

# ...and kill
kill %1




Thanks fr your reply. Look i run a script through cron. lets suppose it works for indefinite time. I just want to kill this process automatically after 5 hours. so what should i do?
can i specify the time with my script when it runs with cron? or any other way?

samengr 02-28-2008 10:23 AM

Quote:

Originally Posted by samengr (Post 3072604)
Thanks fr your reply. Look i run a script through cron. lets suppose it works for indefinite time. I just want to kill this process automatically after 5 hours. so what should i do?
can i specify the time with my script when it runs with cron? or any other way?


Thanks ....

I have done this ....

see the example ... just wrote a small script


runtime=${1:-1m}
mypid=$$
cp /Database/Client_db_200802110231.BAK .&
cpid=$!
sleep $runtime
kill -s SIGTERM $cpid
echo "Both processes are terminated".

I mean now i can define the running time duration of all processes like cp with the time.

Cheers.

jiml8 02-28-2008 06:24 PM

I did it in bash a while back. Did it 2 ways; wallclock time and cpu time. Keep in mind that I was doing it as an exercise, so any rough spots are because it worked for me.

here is the wallclock script:
Code:

#!/bin/bash
#sample script to start a program, permit it to run for a predefined amount of wallclock time, then kill it.  Specify time in seconds
#
progID="gmplayer"
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
maxtime="5"
mins=0
secs=0
killmins=5
startsecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
starthours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
startmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
while [ $secs -lt $killmins ]; do
  cursecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
  curhours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
  curmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
  secs=$(( (curhours-starthours)*3600 + (curmins-startmins)*60 - startsecs + cursecs ))
  sleep 1
done
kill "$mypid"

and here is the cpu time version:

Code:

#!/bin/bash
#sample script to start a program, permit it to run for a predefined amount of CPU time, then kill it.
#
progID="gmplayer"
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
maxtime="5"
mins=0
killmins=5
while [ $mins -lt $killmins ]; do
  usePID=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $4}'`
  hours=`eval echo "$usePID" | awk -F\: '{print $1}'`
  mins=`eval echo "$usePID" | awk -F\: '{print $2}'`
  sleep 1
  echo "$usePID"
  echo "$hours"
  echo "$mins"
done
kill "$mypid"


Tintim 02-17-2010 11:19 AM

Quote:

Originally Posted by jiml8 (Post 3073285)
I did it in bash a while back. Did it 2 ways; wallclock time and cpu time. Keep in mind that I was doing it as an exercise, so any rough spots are because it worked for me.

Here is the script with some corrections:

File Edit Options Buffers Tools Insert Help
Code:

#!/bin/bash                                                                                                                                                                     
#sample script to start a program, permit it to run for a predefined amount of wallclock time, then kill it.  Specify time in seconds                                           
#                                                                                                                                                                               

progID="amarok" # replace by your binary
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
mins=0
secs=0
killmins=300
startsecs=`eval date +%-S `
starthours=`eval date +%-H`
startmins=`eval date +%-M`

#echo "startsecs=$startsecs starthours=$starthours startmins=$startmins"                                                                                                       

while [ $secs -lt $killmins ]; do
    cursecs=`eval date +%-S `
    curhours=`eval date +%-H`
    curmins=`eval date +%-M`
    echo "Aval..."
    secs=$(( (curhours-starthours)*3600 + (curmins-startmins)*60 - startsecs + cursecs ))
    echo "Secs: $secs"
    sleep 1
done
kill "$mypid"
if [ $? -eq 0 ]; then
    echo "Process $mypid was killed"
fi



All times are GMT -5. The time now is 08:29 AM.