forked from ActianCorp/OpenROAD_UnitTestFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_cmd.bash
More file actions
executable file
·47 lines (38 loc) · 849 Bytes
/
timeout_cmd.bash
File metadata and controls
executable file
·47 lines (38 loc) · 849 Bytes
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
41
42
43
44
45
46
#!/bin/bash
# Executes a command with a timeout
# If no timeout occurs the exit status is the exit status of the command
# If the timeout occurs the exit status is 124.
if [ "$#" -lt "2" ]
then
echo "Usage: `basename $0` <timeout (in sec)> <command>"
exit 1
fi
timeout_cleanup()
{
trap - ALRM
kill -ALRM $twpid 2>/dev/null
kill $! 2>/dev/null &&
exit 124
}
timeout_watcher()
{
trap "timeout_cleanup" ALRM
sleep $1 &
wait
kill -ALRM $$
}
#start the timeout_watcher subshell
timeout_watcher $1 &
twpid=$!
# Shift the first parameter - was timeout in seconds
shift
#cleanup after timeout
trap "timeout_cleanup" ALRM INT
# Start the actual command, wait for it to finish and save its status
"$@" &
wait $!
RC=$?
#send ALRM signal to timeout_watcher and wait for it to finish cleanup
kill -ALRM $twpid
wait $twpid
exit $RC