How to get the program to terminate after sertain length of time?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 $!"
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.
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.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.