Skip to content

Commit 46435ac

Browse files
committed
Better detection if the runner is running
1 parent 925c36f commit 46435ac

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

github_actions_runner_manager.sh

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ DEFAULT_RUNNER_DIR="./actions_runner"
99
# directory of the script, see https://stackoverflow.com/a/4774063/19422971
1010
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
1111

12+
# check if runner is running based on the existence of a state file and based
13+
# on a valid PID
14+
is_running () {
15+
local state_path="$1"
16+
17+
# if no PID file, it's not running
18+
if [[ ! -f $state_path/$PID_FILE ]]
19+
then
20+
return 1
21+
fi
22+
23+
local pid=$(cat $state_path/$PID_FILE)
24+
25+
# check PID exists
26+
if ps -p $pid >/dev/null
27+
then
28+
return 0
29+
else
30+
return 1
31+
fi
32+
}
33+
1234
start () {
1335
local runner_path="$1"
1436
local state_path="$2"
@@ -21,20 +43,13 @@ start () {
2143
exit 2
2244
fi
2345

24-
if [[ -f $state_path/$PID_FILE ]]
46+
# check if the runner is running
47+
if is_running "$state_path"
2548
then
26-
# the runner is presumably already running
27-
if $force
28-
then
29-
echo "Force start"
30-
else
31-
echo "The runner is already running"
32-
exit 2
33-
fi
49+
echo "The runner is already running"
50+
exit 2
3451
fi
3552

36-
mkdir -p "$state_path"
37-
3853
# running as a service, see https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service
3954
echo "Starting runner"
4055
echo "Starting runner on $(hostname)" >>$state_path/$LOG_FILE
@@ -49,18 +64,11 @@ start () {
4964
stop () {
5065
local state_path="$1"
5166

52-
if [[ ! -f $state_path/$PID_FILE ]]
67+
# check if the runner is running
68+
if is_running "$state_path"
5369
then
54-
# the runner is not running
55-
echo "The runner is not running"
56-
exit 2
57-
fi
70+
local pid=$(cat $state_path/$PID_FILE)
5871

59-
local pid=$(cat $state_path/$PID_FILE)
60-
61-
# check PID exists
62-
if ps -p $pid >/dev/null
63-
then
6472
echo "Stopping runner"
6573
echo "Stopping runner on $(hostname)" >>$state_path/$LOG_FILE
6674
kill -SIGTERM $pid
@@ -69,14 +77,14 @@ stop () {
6977
echo "Runner already stopped on $(hostname)" >>$state_path/$LOG_FILE
7078
fi
7179

72-
echo "Removing pid file"
73-
rm -f $state_path/$PID_FILE
80+
echo "Removing PID file"
81+
rm --force $state_path/$PID_FILE
7482
}
7583

7684
status () {
7785
local state_path="$1"
7886

79-
if [[ -f $state_path/$PID_FILE ]]
87+
if is_running "$state_path"
8088
then
8189
echo "Runner started"
8290
else
@@ -144,6 +152,8 @@ main () {
144152
done
145153
shift $((OPTIND-1))
146154

155+
mkdir -p "$state_path"
156+
147157
# process mandatory argument
148158
if [[ -n "${1+_}" ]]
149159
then

0 commit comments

Comments
 (0)