-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_script_servicer.sh
executable file
·140 lines (127 loc) · 3.27 KB
/
bash_script_servicer.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
# Bash Script Servicer / Runner - 2014/08/28 - v1.0.0 - Written By: Austin Saint Aubin"
# ∟ Description: Will run a bash script as a service recursively if needed."
# Link to this Script:
# -------------------------------------------------------------------------------------------------
# General Usage: sh "/.../bash_script_servicer.sh" "/../SCRIPT.sh" MODE LOOP_TIME
# General Usage: sh "/volume1/active_system/scripts/bash_script_servicer.sh" "/volume1/active_system/scripts/directory_rsync.sh" start 90
# =================================================================================================
# Notes:
#
# =================================================================================================
# [# Global Static Variables #]
SCRIPT_PATH="$1"
SCRIPT_FILENAME="$(basename $SCRIPT_PATH)"
LOOP_SLEEP_TIME=$3
# Start & Stop Varables
PID_FILE="/var/run/${SCRIPT_FILENAME}.pid"
# [# Global Variables #]
# [# Included Library's & Scripts #]
# PATH="${INSTALL_DIR}/bin:/usr/local/bin:/bin:/usr/bin:/usr/syno/bin"
source /root/.profile # Get Environment Variables from Root Profile
# [# Main Program #] --------------------------------------------------------------------------------------
StartProgram() {
echo "Starting: ${SCRIPT_FILENAME}"
if [ -z $LOOP_SLEEP_TIME ]; then
# Run Program without loop
source "$SCRIPT_PATH"
else
# Loop & Run Program
while sh "$SCRIPT_PATH"; do :; sleep $LOOP_SLEEP_TIME; done
fi
}
# [# Functions #]
DaemonStart() {
DaemonStatus
if [ $? == 0 ]; then
# Run Program / Script
StartProgram &
# Create PID File
echo $! > "$PID_FILE"
else
echo "${SCRIPT_FILENAME} already running."
fi
}
DaemonDebug() {
DaemonStatus
if [ $? == 0 ]; then
echo "Starting: ${SCRIPT_FILENAME}"
# Run Script
MainProgram
fi
}
DaemonStop() {
DaemonStatus
if [ $? == 1 ]; then
echo "Stopping: ${SCRIPT_FILENAME}."
kill $(cat "$PID_FILE");
rm -f "$PID_FILE"
sleep 3
else
echo "Nothing to stop for: ${SCRIPT_FILENAME}."
fi
}
DaemonStatus() {
# Check if PID file exist
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if [ -n "$(ps | grep $PID | grep -vn "grep $PID")" ]; then
echo "${SCRIPT_FILENAME} is running ..."
return 1 # is running
else
echo "${SCRIPT_FILENAME} is NOT running ..."
rm -f ${PID_FILE} # Remove Invalid PID
return 0 # is NOT running
fi
else
echo "${SCRIPT_FILENAME} is NOT running ...."
return 0 # is NOT running
fi
}
# [# Service Control #] --------------------------------------------------------------------------------------
case $2 in
start)
DaemonStart
sleep 1
DaemonStatus
exit $(( ! $? )) # [ $? == 1 ] && exit 0 || exit 1 # this if statement flips the boolean outcome.
;;
stop)
DaemonStop
sleep 1
DaemonStatus
exit $?
;;
restart)
DaemonStop
sleep 10
DaemonStart
sleep 1
DaemonStatus
exit $(( ! $? )) # this if statement flips the boolean outcome.
;;
status)
DaemonStatus
exit $(( ! $? )) # this if statement flips the boolean outcome.
;;
debug)
DaemonDebug
exit 0
;;
log-show)
cat "$LOGSTASH_LOG_PATH"
exit 0
;;
log-clear)
rm -f "$LOGSTASH_LOG_PATH"
exit 0
;;
log)
echo "$LOGSTASH_LOG_PATH"
exit 0
;;
*)
echo "Usage: $0 {script_path} {start|stop|restart|status|debug|log|log-show|log-clear}"
exit 1
;;
esac