forked from VOLTTRON/volttron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_agent_running_in_isolation.sh
executable file
·114 lines (98 loc) · 3.29 KB
/
stop_agent_running_in_isolation.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# Function to stop a process using the given kill signal and wait at most for the given number of seconds
stop_process_and_wait(){
local pid=$1
local signal=$2
local count
((count=$3*2))
local timeout=0
kill -s $signal $pid
exit_code="$?"
if [ $exit_code -ne 0 ]; then
echo "Agent stop failed"
return 1
fi
# wait for agent to stop
i=0
while kill -0 "$pid" ; do
sleep 0.5
let "i++"
if [ $i -eq $count ]; then
timeout=1
break
fi
done
return $timeout
}
user=$1
pid=$2
# Verify script is run as root or using sudo because agents will be running as root in agent isolation mode
if [ -z "$UID" ] || [ $UID -ne 0 ]; then
echo "Script should be run as root user or as sudo <path to this script>/stop_agent_running_in_isolation.sh"
exit
fi
# user running the agent should be of the format volttron_[0-9]+
# Agent users created by platform have prefix volttron_<timestamp>
re='volttron_[0-9]+'
if ! [[ $user =~ $re ]]; then
echo "Invalid user $user"
echo "Usage: <path>/stop_agent_running_in_isolation.sh <user name of requester> <pid of agent to be stopped>"
exit 1
fi
# Check if pid is passed and it is a number
re='^[0-9]+$'
if [ -z "$pid" ] || ! [[ $pid =~ $re ]]; then
echo "Invalid process id..."
echo "Usage: <path>/stop_agent_running_in_isolation.sh <user name of requester> <pid of agent to be stopped>"
exit 2
fi
# Attempt to find process corresponding to pid
command=`ps -h -p $pid -o command`
exit_code="$?"
# If exit code is not 0 for the ps command then no such pid exists
if [ $exit_code -ne 0 ]; then
echo "Invalid process id $pid. Process id does not correspond to any valid agent process"
echo "Usage: <path>/stop_agent_running_in_isolation.sh <user name of requester> <pid of agent to be stopped>"
exit 3
fi
# if we find a command check the pattern
# command should be of the pattern
# sudo -E -u <username passed to this script> /<some path to volttron source>/env/bin/python -m <agent name>
re="^sudo -E -u $user /.+/python.* -m .+"
if [[ ! $command =~ $re ]]; then
echo "Invalid process id. pid does not correspond to a volttron agent owned by user $user"
echo "Usage: <path>/stop_agent_running_in_isolation.sh <user name of requester> <pid of agent to be stopped>"
exit 4
fi
agent_pid=`pgrep -P $pid `
exit_code="$?"
# If exit code is not 0 for the ps command then no such pid exists
if [ $exit_code -ne 0 ]; then
echo "Invalid process id $pid. Process id does not have valid child process"
echo "Usage: <path>/stop_agent_running_in_isolation.sh <user name of requester> <pid of agent to be stopped>"
exit 5
fi
echo "Sending SIGINT signal to $agent_pid "
# Attempt 1 send SIGINT give process to complete onstop functions
stop_process_and_wait $agent_pid SIGINT 60
if [ $? -eq 0 ]; then
echo "Agent stopped"
exit 0
fi
echo "Sending SIGTERM signal to $agent_pid "
# Attempt 2 send terminate
stop_process_and_wait $agent_pid SIGTERM 30
if [ $? -eq 0 ]; then
echo "Agent terminated"
exit 0
fi
echo "Sending SIGKILL signal to $agent_pid "
# Attempt 3 kill
stop_process_and_wait $agent_pid SIGKILL 30
if [ $? -eq 0 ]; then
echo "Agent killed"
exit 0
else
echo "Unable to stop/kill agent "
exit 5
fi