-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestart_process.sh
More file actions
53 lines (48 loc) · 1.26 KB
/
restart_process.sh
File metadata and controls
53 lines (48 loc) · 1.26 KB
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
48
49
50
51
52
53
#!/bin/bash
#
# Run given process in a loop and restart on errors
# Process is run until this process is interrupted or the child process succeeds
echo "Critical warning: Only run this script if you intend to."
echo "Due to the evaluation of passed arguments malicious code can be executed on your computer."
sleep 2
echo "Warning: This script runs indefinitely until exit code is 0."
echo "Simple interruption will stop it (Ctrl+C)."
sleep 1
###
# Runs given command and restarts it until finish
# Arguments:
# Command and its arguments to run with restarting
# Returns:
# Process stdout & stderr, exit 0 on success
###
restart_process() {
if [[ -z $1 ]]; then
echo "Usage: restart_process <command> <args>"
exit 1
fi
local exit_code=0
while true; do
($@)
exit_code=$?
if [[ $exit_code -eq 0 ]]; then
echo "Process finished successfully"
break
else
echo "Process crashed with exit code $exit_code. Restarting..."
sleep 1
fi
done
}
###
# Main script method
# Arguments:
# Command and its arguments to run with restarting
# Returns:
# Same as restart_process
###
main() {
restart_process "$@"
}
if [[ ${BASH_SOURCE[0]} == ${0} ]]; then
main "$@"
fi