LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-28-2008, 06:39 AM   #1
samengr
Member
 
Registered: Jan 2008
Posts: 59

Rep: Reputation: 15
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.
 
Old 02-28-2008, 07:49 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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
 
Old 02-28-2008, 07:59 AM   #3
samengr
Member
 
Registered: Jan 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by marozsas View Post
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?
 
Old 02-28-2008, 10:23 AM   #4
samengr
Member
 
Registered: Jan 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by samengr View Post
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.
 
Old 02-28-2008, 06:24 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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"
 
Old 02-17-2010, 11:19 AM   #6
Tintim
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by jiml8 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill the process running on specific port in linux rajaniyer123 Linux - General 2 12-01-2009 05:35 PM
Log Out a Specific User (KILL) carlosinfl Linux - General 8 08-29-2007 10:53 AM
kill specific process fj8283888 Linux - General 6 05-31-2007 06:28 AM
cannot kill process (kill -9 does not work) mazer13a Linux - General 1 05-27-2005 02:32 PM
kill a specific session in windows rabeea General 8 12-15-2004 03:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration