starting processes with timeout?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers starting processes with timeout?
# 1  
Old 05-05-2006
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?
# 2  
Old 05-05-2006
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?
# 3  
Old 05-05-2006
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.
# 4  
Old 05-06-2006
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.
# 5  
Old 05-07-2006
i have two solutions for the approach ... Smilie Smilie Smilie

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
# 6  
Old 05-07-2006
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.
# 7  
Old 05-07-2006
This is an odd thing to do. But I just got this script to work:
Code:
#! /usr/bin/ksh

sleep 200 ; kill -term -$$ &
sleep 7
kill -term -$$

One of my sleep processes represents a process that might run too long. The other sleep process is the timer. Either process could play either role, but I envisioned that first line as the timer and the second as the process to be timed. One of the processes will probably finish first and then run the "kill -term -$$". This will kill the process group. On a multiprocessor system simultaneous kills may occur, but the kernel will ensure that they happen serially. There is no race condition here. This depends on the shell putting all processes in a script into a single process group. So neither sleep process could be replaced with another shell script because it would become a new process group.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

3. AIX

init not starting processes from inittab

Hello, I'm having a problem starting the cron daemon automatically from inittab, let me provide the details below: We are having five equally installed machines. One of them was upgraded in the past, one we upgraded recently, both from 5300-05-06 to 5300-07-01-0748. On the upgraded... (6 Replies)
Discussion started by: hybr1d
6 Replies

4. SCO

Starting processes automatically on bootup

Hello - We have setup printers to startup in the /etc/rc.d/8 directory and when the SCO system is booted this script should kick them off or start them up but it doesn't. So I have to start it from root and leave the window open. I know this isn't right and trying to figure out what's missing. ... (13 Replies)
Discussion started by: dbm7230
13 Replies

5. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

6. Red Hat

RPC Timeout

I have a RHEL 2.1 machine that I am trying to get to mount a remote nfs filesystem. Both servers have 2 network interfaces. My linux machine can mount the filesystem through one interface with no problems but if I switch over and try to mount it through the other interface using a totally... (1 Reply)
Discussion started by: darren.wyatt
1 Replies

7. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

8. Solaris

About the Timeout

Hello everyone I am a new one,I want to know how to get the solaris force the loginer out if he do not in a time thanks (4 Replies)
Discussion started by: lyh003473
4 Replies

9. HP-UX

timeout

How can I kick a user out after being idle for a certain amount of time, would prefer not to use scripts, will TMOUT work on HP-UX? (5 Replies)
Discussion started by: csaunders
5 Replies

10. UNIX for Dummies Questions & Answers

Starting Processes

I am currently running SCO OpenServer. When the machine is restart it automatically intitiates processes that allow me to use the ARCserv backup software... Recently, these process were killed and I would like to restart them. The problem is I dont know the name or location of the files invoked... (1 Reply)
Discussion started by: LowOrderBit
1 Replies
Login or Register to Ask a Question