-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_install.sh
More file actions
executable file
·157 lines (135 loc) · 4.13 KB
/
init_install.sh
File metadata and controls
executable file
·157 lines (135 loc) · 4.13 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
#---------------------------------EDIT HERE-----------------------------------------
name="webhook"
shot_desc="Deployment webhook"
desc="A simple webhook written in Go."
user="$(whoami)" #the user name that will run the script
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #get this bash script directory
script_path="$dir/$name"
#Uncomment this line to set the path of a configuration file
script_config="$dir/config.json"
daemon_pwd="/usr/local/bin"
daemon_path="/usr/local/bin/$name"
daemon_config="/etc/$name"
daemon_opts="-port 9000 -hooks $daemon_config/${name}.config -verbose"
#-----------------END OF EDIT UNLESS YOU KNOW WHAT YOU ARE DOING--------------------
if [[ $# -eq 1 && "$1" = "-u" ]]; then
sudo service "$name" stop
sudo rm "$daemon_path"
sudo rm -rf "$daemon_config"
sudo rm "/etc/init.d/$name"
sudo update-rc.d "$name" remove
exit
fi
service="#!/bin/sh
### BEGIN INIT INFO
# Provides: $name
# Required-Start: \$local_fs \$remote_fs \$network
# Required-Stop: \$local_fs \$remote_fs \$network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: $shot_desc
# Description: $desc
### END INIT INFO
# Documentation available at
# http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html
# Debian provides some extra functions though
. /lib/lsb/init-functions
DAEMON_NAME=\"$name\"
DAEMON_USER=\"$user\"
DAEMON_PATH=\"$daemon_path\"
DAEMON_OPTS=\"$daemon_opts\"
DAEMON_PWD=\"$daemon_pwd\"
DAEMON_PID=\"/var/run/\${DAEMON_NAME}.pid\"
DAEMON_LOG=\"/var/log/\${DAEMON_NAME}.log\"
DAEMON_DESC=\$(get_lsb_header_val \$0 \"Short-Description\")
DAEMON_NICE=0
[ -r \"/etc/default/\${DAEMON_NAME}\" ] && . \"/etc/default/\${DAEMON_NAME}\"
do_start() {
local result
pidofproc -p \"\${DAEMON_PID}\" \"\${DAEMON_PATH}\" > /dev/null
if [ \$? -eq 0 ]; then
log_warning_msg \"\${DAEMON_NAME} is already started\"
result=0
else
log_daemon_msg \"Starting \${DAEMON_DESC}\" \"\${DAEMON_NAME}\"
touch \"\${DAEMON_LOG}\"
chown \$DAEMON_USER \"\${DAEMON_LOG}\"
chmod u+rw \"\${DAEMON_LOG}\"
if [ -z \"\${DAEMON_USER}\" ]; then
start-stop-daemon --start --quiet --oknodo --background \\
--nicelevel \$DAEMON_NICE \\
--chdir \"\${DAEMON_PWD}\" \\
--pidfile \"\${DAEMON_PID}\" --make-pidfile \\
--startas /bin/bash -- -c \"exec \${DAEMON_PATH} \${DAEMON_OPTS} >> \${DAEMON_LOG} 2>&1\"
result=\$?
else
start-stop-daemon --start --quiet --oknodo --background \\
--nicelevel \$DAEMON_NICE \\
--chdir \"\${DAEMON_PWD}\" \\
--pidfile \"\${DAEMON_PID}\" --make-pidfile \\
--chuid \"\${DAEMON_USER}\" \\
--startas /bin/bash -- -c \"exec \${DAEMON_PATH} \${DAEMON_OPTS} >> \${DAEMON_LOG} 2>&1\"
result=\$?
fi
log_end_msg \$result
fi
return \$result
}
do_stop() {
local result
pidofproc -p \"\${DAEMON_PID}\" \"\${DAEMON_PATH}\" > /dev/null
if [ \$? -ne 0 ]; then
log_warning_msg \"\${DAEMON_NAME} is not started\"
result=0
else
log_daemon_msg \"Stopping \${DAEMON_DESC}\" \"\${DAEMON_NAME}\"
killproc -p \"\${DAEMON_PID}\" \"\${DAEMON_PATH}\"
result=\$?
log_end_msg \$result
rm \"\${DAEMON_PID}\"
fi
return \$result
}
do_restart() {
local result
do_stop
result=\$?
if [ \$result = 0 ]; then
do_start
result=\$?
fi
return \$result
}
do_status() {
local result
status_of_proc -p \"\${DAEMON_PID}\" \"\${DAEMON_PATH}\" \"\${DAEMON_NAME}\"
result=\$?
return \$result
}
do_usage() {
echo \$\"Usage: \$0 {start | stop | restart | status}\"
exit 1
}
case \"\$1\" in
start) do_start; exit \$? ;;
stop) do_stop; exit \$? ;;
restart) do_restart; exit \$? ;;
status) do_status; exit \$? ;;
*) do_usage; exit 1 ;;
esac
"
printf "$service" > "/tmp/$name"
#copy the executable to local/bin
sudo cp "$script_path" "$daemon_path"
sudo chmod +x "$daemon_path"
#copy the configuration if it is declared
sudo mkdir -p "$daemon_config"
if [[ -n "${script_config}" ]]; then
sudo cp "$script_config" "$daemon_config/${name}.config"
fi
#install the init script
sudo mv "/tmp/$name" "/etc/init.d/$name"
sudo chmod +x "/etc/init.d/$name"
sudo service "$name" start
sudo update-rc.d "$name" defaults