In Linux I would like to run a program but only for a limited time, like 1 second. If the program exceeds this running time I would like to kill the process and show an error message.
7 Answers
Ah well. timeout(1)
.
DESCRIPTION
Start COMMAND, and kill it if still running after DURATION.

- 25,009
- 4
- 84
- 97
-
2Please comment on downvoting. – wRAR Apr 05 '10 at 13:00
-
5I think there was some misunderstanding here. a simple "man timeout" can show this answer is the most accurate answer (though somewhat concise) to the question at hand. – Yoav Weiss Jun 14 '11 at 20:57
-
4timeout(1) is in GNU coreutils. – minopret Feb 23 '12 at 16:16
StackOverflow won't allow me to delete my answer since it's the accepted one. It's garnering down-votes since it's at the top of the list with a better solution below it. If you're on a GNU system, please use timeout
instead as suggested by @wRAR. So in the hopes that you'll stop down-voting, here's how it works:
timeout 1s ./myProgram
You can use s
, m
, h
or d
for seconds (the default if omitted), minutes, hours or days. A nifty feature here is that you may specify another option -k 30s
(before the 1s
above) in order to kill it with a SIGKILL after another 30 seconds, should it not respond to the original SIGTERM.
A very useful tool. Now scroll down and up-vote @wRAR's answer.
For posterity, this was my original - inferior - suggestion, it might still be if some use for someone.
A simple bash-script should be able to do that for you
./myProgram &
sleep 1
kill $! 2>/dev/null && echo "myProgram didn't finish"
That ought to do it.
$!
expands to the last backgrounded process (through the use of &
), and kill returns false if it didn't kill any process, so the echo is only executed if it actually killed something.
2>/dev/null
redirects kill's stderr, otherwise it would print something telling you it was unable to kill the process.
You might want to add a -KILL
or whichever signal you want to use to get rid of your process too.
EDIT
As ephemient pointed out, there's a race here if your program finishes and the some other process snatches the pid, it'll get killed instead. To reduce the probability of it happening, you could react to the SIGCHLD and not try to kill it if that happens. There's still chance to kill the wrong process, but it's very remote.
trapped=""
trap 'trapped=yes' SIGCHLD
./myProgram &
sleep 1
[ -z "$trapped" ] && kill $! 2>/dev/null && echo '...'

- 34,597
- 9
- 72
- 86
-
1Might want to save `PID=$!`, set a `trap 'PID=' SIGCHLD`, and `[ -n $PID ] && kill $pid`, so that in the (unlikely) case that your child exits and another process is started reusing the same PID, you don't kill the innocent bystander. – ephemient Mar 05 '10 at 15:57
-
@ephemient: yeah, I considered that too. won't eliminate the race completely, but it'll certainly shrink the window. – falstro Mar 05 '10 at 16:47
-
I actually don't think there's a window in the new procedure, because the PID can only be reused after the original process has been reaped, which (if we haven't trapped) hasn't happened yet. – ephemient Mar 05 '10 at 18:14
-
@ephemient: I'm not sure exactly how bash handles it, you might be right. Question is if it could execute the trap after spawning the kill process, but before that process got to the killing... – falstro Mar 05 '10 at 20:43
-
Bash is single-threaded and `kill` is a built-in, so I think that scenario is unlikely. But I suppose that if it doesn't `sigprocmask` while running the whole `&&` (and I haven't checked sources, so I'm not sure), then *that's* where a tiny window could lie... – ephemient Mar 05 '10 at 22:41
-
4Downvote because answer from @wRAR seems more concise and spot-on. Also, cleaner. – Thomas Daugaard Oct 11 '13 at 15:56
-
-
Worth noting: This script will cause the time limit to pass for sure before the script terminates even if your process terminates before the time limit. – ben Jun 04 '16 at 16:33
Maybe CPU time limit (ulimit -t
/setrlimit(RLIMIT_CPU)
) will help?

- 25,009
- 4
- 84
- 97
-
-
@Overdeath: You'd probably have to make your own program `fork`, `setrlimit` and `waitpid`, and then check if it got killed by a SIGXCPU. You could also check the return value of your program, bash usually sets `$?` to something high when it's killed by a signal. Don't know if it possible to determine whether it's SIGXCPU though. – falstro Mar 05 '10 at 15:51
-
@Overdeath It will be killed by SIGXCPU if it exceeds the softlimit; if it ignores that and hits the hardlimit, it will be killed by SIGKILL. In shell, $? will be 128+signal if the process was killed by a signal; in C, check `WIFSIGNALED(status)` and `WTERMSIG(status)`, if `status` was returned by `wait*`. – ephemient Mar 05 '10 at 15:52
-
This is the best solution. What I do is wrap the command as follows in parentheses: (ulimit -t 5; commandToDo args) This nicely kills the command after 5 seconds. Works in Linux and OS X. – Timothy C. Lethbridge Nov 16 '14 at 00:35
you could launch it in a shell script using &
your_program &
pid=$!
sleep 1
if [ `pgrep $pid` ]
then
kill $pid
echo "killed $pid because it took too long."
fi
hope you get the idea, I'm not sure this is correct my shell skills need some refresh :)

- 3,814
- 1
- 23
- 30
-
1This method is dangerous due to a possible race condition: What if the process finished within one second and *another* process got assigned its original PID? You then would kill the wrong process. – Binarus Oct 30 '20 at 07:28
tail -f file & pid=$!
sleep 10
kill $pid 2>/dev/null && echo '...'

- 1,502
- 5
- 18
- 30
-
2This method is dangerous due to a possible race condition: What if the process finished within 10 seconds and *another* process got assigned its original PID? You then would kill the wrong process. – Binarus Oct 30 '20 at 07:29
If you have the sources, you can fork()
early in main()
and then have the parent process measure the time and possibly kill the child process. Just use standard system calls fork()
, waitpid()
, kill()
, ... maybe some standard Unix signal handling. Not too complicated but takes some effort.
You can also script something on the shell although I doubt it will be as accurate with respect to the time of 1 second.
If you just want to measure the time, type time <cmd ...>
on the shell.

- 1,935
- 11
- 9
-
This was the way i thought of doing it in the first time. But I need the program to work without access to the source files. Thanks for the answer – Overdeath Mar 05 '10 at 15:24
Ok, so just write a short C program that fork
s, calls execlp
or something similar in the child, measures the time in the parent and kill
s the child. Should be easy ...

- 1,935
- 11
- 9