|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
starting processes with timeout?
Does anyone know it it is possible to start a program with a timeout, so that it is automatically killed if the timeout expires? If yes, how?
|
| Sponsored Links | ||
|
|
|
|||
|
You can do this, but do not do it as a privleged user, there is a risk of clobbering a valid running process. Code:
/path/to/myprogram arg1 arg2 & last_pid=$! sleep 100 # wait one hundred seconds kill -TERM $last_pid # this will kill only processes you own - if you're not privileged This whole thing is not the best possible idea - are you sure sending SIGTERM to these processes is not going to wreck some data? |
|
|||
|
I think this should be OK in my case.
The process in this case is an optimization program with an indata file. I have a shell script which runs this program for a bunch of different indata files in a directory. The reason I wanted a timeout is that the runtime of the program is very large in some cases and then I have to manually kill them. That's why I wanted to use a timeout so that this issue is handled automatically. I will add this code in my shell script and try it. Thanks. Out of curiousity, do you know any other way? What happens if the process has already finished before sleep is finished (i.e. before the timeout)? Then we try to kill a nonexisting pid. |
|
|||
|
The problem is: on a busy system the pid may possibly be another process. You would in trouble if that process were a process you owned. So, try to keep the sleep value reasonably close to what your process actually needs to complete its job.
|
|
|||
|
i have two solutions for the approach ...
![]() first) once you are sure that the process you have initiated to be killed after 'x' seconds.. then inbuilt a infinite loop logic in the program you run ... after it had completed all its required computations let the program to run in a infinite loop so no chance of another valid process being killed. second) prior to sending SIGTERM signal to the process checking for the time stamp that is for sure would prevent terminating a valid process |
|
|||
|
Actually my point of view is:
If you need to run the job, run the job to completion, don't impose artificial time constraints. If the process takes too long, try either threads or multitasking - dividing the job into smaller parts. Or, if you can kill the job safely, that means you don't really need the job scheduled as it is anyway. nice it and let it run all day in background. This kill it model of job control is not good. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Identifying and grouping OS processes and APP processes | wilsonee | Solaris | 2 | 09-30-2008 10:26 AM |
| Monitoring Processes - Killing hung processes | ukndoit | UNIX for Advanced & Expert Users | 4 | 01-17-2008 04:30 AM |
| ssh/scp - can you specify timeout? | frustrated1 | Shell Programming and Scripting | 3 | 01-02-2008 07:57 PM |
| About the Timeout | lyh003473 | Solaris | 4 | 07-24-2004 10:12 PM |
| Starting Processes | LowOrderBit | UNIX for Dummies Questions & Answers | 1 | 09-06-2001 06:02 PM |