LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get the program to terminate after sertain length of time? (https://www.linuxquestions.org/questions/programming-9/how-to-get-the-program-to-terminate-after-sertain-length-of-time-437754/)

hubabuba 04-22-2006 12:02 PM

How to get the program to terminate after sertain length of time?
 
Hi!

I want my program to run for 5 minutes (or any other length of time), during which it will be doing a task, then it must terminate after 5 minutes or any other length of time. Is there such function that allows time setting?

I am not shure, but would time() or timer() functions be used for this?

Tnanks

paulsm4 04-22-2006 12:58 PM

Hi -

Probably the easiest way is to use a shell script:
1. The script starts the program in the background (&) and saves the PID ($!)
2. The program executes while the script sleeps N seconds
3. At the end of N seconds, the script does a "kill $!"

Here's an example:
Code:

MYPROG &
PID=$!
sleep 5
kill -9 $PID

A second approach is for your C/C++ program to set a signal handler for SIGALRM, then call "alarm ()". Here's an example:
http://www.cs.usfca.edu/benson/cs326.../src/sigalrm.c

'Hope that helps .. PSM

exvor 04-22-2006 01:05 PM

U might be able to use the time function as well. if doing it in C of course. U could get the current time and then add 5 min to it then have a while loop check it every tick till it matches the correct value.

This might be problematic and there is probably a better way like paul said above.

hubabuba 04-23-2006 01:52 PM

Thanks, sorry I forgot to mention, I'm using C.

Dark_Helmet 04-23-2006 02:29 PM

Just throwing out another option...

You could make the program multi-threaded. Create a thread that just sleeps the desired amount of time, and then communicates in some manner (global variable, a system "kill" command, or whatever) that time is up to the other thread(s).

It's basically the same as paulsm4's sigalarm example. For me, I never became comfortable with signals and generally avoid them. That's just me though.

jiml8 04-23-2006 10:19 PM

The problem with having the program monitor its own time is that if the program runs wild you can't trust the monitoring.

You might want to do it from outside the program, perhaps using a shell script that runs maybe once a second until a time condition on the program being monitored is satisfied. When that time condition is satisfied, the shell script kills the program and exits.

Perhaps something like this:
Code:

progID="myprog"
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
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
done
kill "$mypid"

By the way. I actually tested this and it works. It considers CPU time, not wallclock time.

jiml8 04-23-2006 11:06 PM

Since I became amused by this script, I wrote another variant of it. This variant runs on wallclock time. It starts the program and kills it after a specified number of seconds.

It could be made more efficient; I should call up the current time just once at the beginning and once each time through the loop, saving it into a variable, then extracting hours minutes and seconds via awk.
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="myprog"
$progID &
mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
echo "mypid $mypid"
maxtime="5"
mins=0
secs=0
killsecs=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 $killsecs ]; 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"



All times are GMT -5. The time now is 04:23 PM.