-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill.sh
More file actions
executable file
·83 lines (76 loc) · 3.01 KB
/
Copy pathkill.sh
File metadata and controls
executable file
·83 lines (76 loc) · 3.01 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Causes the running screen to exit via SIGTERM.
# Dependencies:
# - screen
# Child scripts:
# - attach.sh
# Configuration variables:
# - RUNAS: The user with whom the screen would launch.
# - SCREEN: The name of the screen that the server would run under.
# Exit codes:
# - 71 (EX_OSERR): The server did not respond to SIGKILL
# - 127 (EX_CMDNOTFOUND): Missing a dependency
# Navigate to the directory of the env script and trap exit for popd
pushd "$(dirname "$0")" >/dev/null
# Configure the environment
. ./env.sh
# Add trap for exit
prepend_trap 'popd >/dev/null' EXIT
depends screen
if ! sudo -u $RUNAS screen -ls | grep -q $SCREEN; then
fatal $EX_OK "There was no screen $SCREEN present for $RUNAS."
fi
# Get the screen daemon's PID from `screen -ls` itself rather than assuming
# a socket directory layout, which varies by distro.
screen_pid=$(sudo -u $RUNAS screen -ls | grep -oP "\d+(?=\.${SCREEN}(\s|$))" | head -1)
# Filter by ppid ourselves; `ps -u X --ppid Y` silently ignores --ppid on some systems.
pid=$(ps h -u $RUNAS -o pid,ppid | awk -v ppid="$screen_pid" '$2 == ppid {print $1}' | head -1)
if ps -p $pid >/dev/null; then
cmd=$(ps h -p $pid -o cmd)
log "Found process $pid with command $cmd, sending SIGTERM..."
sudo -u $RUNAS kill -TERM $pid
log 'Waiting 1 minute for process exit.'
for ((i=240;i>0;i--)); do
if [ $i -gt 120 ]; then
spinner 'Checking for process status in 1 minute '
elif [ $i -gt 60 ]; then
spinner 'Checking for process status in 30 seconds'
elif [ $i -gt 40 ]; then
spinner 'Checking for process status in 15 seconds'
elif [ $i -ge 20 ]; then
spinner 'Checking for process status in 10 seconds'
elif [ $i -gt 4 ]; then
spinner 'Checking for process status in %s seconds' $(($i / 4 + 1))
else
spinner 'Checking for process status in 1 second '
fi
sleep 0.25s
done
ovr 'Checking for process status now. \n'
if ps -p $pid >/dev/null; then
warning "Process $pid failed to respond to SIGTERM in time, sending SIGKILL..."
sudo -u $RUNAS kill -KILL $pid
log 'Waiting 10 seconds for process death.'
for ((i=40;i>0;i--)); do
if [ $i -ge 20 ]; then
spinner 'Checking for process status in 10 seconds'
elif [ $i -gt 4 ]; then
spinner 'Checking for process status in %s seconds' $(($i / 4 + 1))
else
spinner 'Checking for process status in 1 second '
fi
sleep 0.25s
done
ovr 'Checking for process status now. \n'
if ps -p $pid >/dev/null; then
fatal $EX_OSERR 'Failed to terminate run script!'
fi
fi
log 'Run script terminated. Reattaching screen process in case it is still active.'
./attach.sh
else
warning 'No process to kill could be found.'
fi
if [ -f .watchdog_lock ]; then
rm -f .watchdog_lock >/dev/null
fi