forked from bytekid/ctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout
executable file
·40 lines (36 loc) · 1013 Bytes
/
timeout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# 2008 by Ortwin Glueck
# Permission to use, copy, modify, redistribute in any form is hereby granted to anyone.
# http://www.odi.ch/prog/timeout.php
if [ $# -lt 2 ]; then
echo "Starts a program and kills it if it is still alive"
echo "after a given time."
echo "Syntax: timeout [-signal] timespec program [arguments]"
echo " signal the signal to send to the process, default is 9 (kill argument)"
echo " timespec time in seconds (sleep argument)"
echo " program the program to execute"
echo " arguments the arguments for program"
echo "The exit code is preserved or 127 if it could not be determined"
exit 1
fi
if [ "${1:0:1}" = "-" ]; then
SIGNAL="${1}"
shift
else
SIGNAL="-9"
fi
TIME=${1}
shift
# start program in the background
$@ &
PID=$!
if [ "${PID}" = "0" ]; then
# process has already ended
exit 127;
fi
(sleep "${TIME}"; kill "${SIGNAL}" "${PID}") & 2>/dev/null
KILLER=$!
wait "${PID}" 2>/dev/null
R=$?
kill -HUP "${KILLER}" 2>/dev/null
exit ${R}