-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepalive.sh
More file actions
executable file
·42 lines (34 loc) · 826 Bytes
/
keepalive.sh
File metadata and controls
executable file
·42 lines (34 loc) · 826 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
#!/usr/bin/env bash
# keepalive script for restarting a process when it breaks
# Usage: pass in a process name
args=("$@")
if [ $# -eq 0 ] || [ $# -gt 1 ]; then
echo "Correct usage:"
echo " keepalive [script/process]"
else
process=${args[0]}
PID=""
if ! pgrep -f '$process' 2>&1 > /dev/null; then
echo "$process wasn't running."
echo "starting $process"
$process
PID=$!
else
echo "$process was already running"
echo "attaching to $process"
PID=$(pgrep -f '$process')
fi
function terminate {
kill $PID
exit
}
trap terminate SIGHUP SIGINT SIGKILL SIGTERM SIGSTOP
while true; do
while ps aux|grep -v "grep --color=auto"|grep -v 'keepalive.sh'|grep "$process" > /dev/null; do
sleep 1
done
echo -e "$(date): Restarting $process\n" >> ~/logs/keepalive.log
$process
PID=$!
done
fi