File: run_with_timeout.sh

package info (click to toggle)
dfdatetime 20200824-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 736 kB
  • sloc: python: 5,827; sh: 120; makefile: 18
file content (47 lines) | stat: -rwxr-xr-x 969 bytes parent folder | download | duplicates (11)
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
47
#!/bin/bash
#
# Script to run commands on a Travis-CI test VM that otherwise would time out
# after 10 minutes. This replaces travis_wait and outputs stdout of the command
# running.
#
# This file is generated by l2tdevtools update-dependencies.py, any dependency
# related changes should be made in dependencies.ini.

# Exit on error.
set -e

# Usage: ./run_with_timeout.sh [TIMEOUT] [COMMAND] [OPTION] [...]

TIMEOUT=$1;
shift

# Launch a command in the background.
$* &

PID_COMMAND=$!;

# Probe the command every minute.
MINUTES=0;

while kill -0 ${PID_COMMAND} >/dev/null 2>&1;
do
	# Print to stdout, seeing this prints a space and a backspace
	# there is no visible trace.
	echo -n -e " \b";

	if test ${MINUTES} -ge ${TIMEOUT};
	then
		kill -9 ${PID_COMMAND} >/dev/null 2>&1;

		echo -e "\033[0;31m[ERROR] command: $* timed out after: ${MINUTES} minute(s).\033[0m";

		exit 1;
	fi
	MINUTES=$(( ${MINUTES} + 1 ));

	sleep 60;
done

wait ${PID_COMMAND};

exit $?;