-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildctrl.sh
executable file
·202 lines (175 loc) · 3.46 KB
/
buildctrl.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
#
# Copyright (c) 2021-2022 brian
#
# Short: https://git.io/csh-buildctrl.sh
# File name: lego.sh
# Description: Build control.sh
# System Required: GNU/Linux
# Version: 1.0
##############################################################
## Project Vars
BIN_FILE=""
BIN_PARAM=""
SRV_NAME=""
# fonts color
ERRO(){
echo -e "\033[31m\033[01m$1\033[0m"
}
INFO(){
echo -e "\033[32m\033[01m$1\033[0m"
}
WARN(){
echo -e "\033[33m\033[01m$1\033[0m"
}
SUCC(){
echo -e "\033[34m\033[01m$1\033[0m"
}
BOLD(){
echo -e "\033[1m\033[01m$1\033[0m"
}
##############################################################
## Project code
build_control() {
cat <<EOF > control.sh
#!/bin/bash
binfile=BIN_FILE
cwd=\$(
cd $(dirname \$0)/
pwd
)
cd \$cwd
usage() {
echo \$"Usage: \$0 {start|stop|restart|status}"
exit 0
}
start() {
if [ ! -f \$binfile ]; then
echo "file[\$binfile] not found"
exit 1
fi
if [ \$(pidof "\$binfile") ]; then
echo "\${binfile} already started"
return
fi
# add custom cmd for start service
echo "[run] \$cwd/\$binfile BIN_PARAM &> stdout.log &"
nohup \$cwd/\$binfile BIN_PARAM &> stdout.log &
for ((i = 1; i <= 15; i++)); do
if [ \$(pidof "\$binfile") ]; then
echo "\${binfile} started"
return
fi
sleep 0.5
done
echo "cannot start \${binfile}"
exit 1
}
stop() {
if [ ! \$(pidof "\$binfile") ]; then
echo "\${binfile} already stopped"
return
fi
pidof "\$binfile" | xargs kill
for ((i = 1; i <= 15; i++)); do
if [ ! \$(pidof "\$binfile") ]; then
echo "\${binfile} stopped"
return
fi
sleep 0.5
done
echo "cannot stop \${binfile}"
exit 1
}
restart() {
stop
start
status
}
status() {
ps aux | grep -v grep | grep \${binfile}
echo "pid: \$(pidof \$binfile)"
}
case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
usage
;;
esac
EOF
sed -i "s/BIN_FILE/${BIN_FILE}/g" ./control.sh
sed -i "s/BIN_PARAM/${BIN_PARAM}/g" ./control.sh
chmod +x ./control.sh
INFO "usage: control.sh"
BOLD "./control.sh start | stop | status | reload"
}
build_service() {
cwd=$(
cd ./
pwd
)
if [ ! -n "${SRV_NAME}" ]; then
SRV_NAME=${BIN_FILE}
fi
cat <<EOF > ${SRV_NAME}.service
[Unit]
Description="${SRV_NAME}"
[Service]
Type=simple
ExecStart=$cwd/${BIN_FILE}
WorkingDirectory=$cwd
Restart=always
RestartSecs=1s
SuccessExitStatus=0
LimitNOFILE=65536
StandardOutput=file:/var/log/${BIN_FILE}.log
StandardError=file:/var/log/${BIN_FILE}_error.log
SyslogIdentifier=${SRV_NAME}
[Install]
WantedBy=multi-user.target
EOF
INFO "\nusage: ${SRV_NAME}.service"
BOLD "cp -a ${SRV_NAME}.service /usr/lib/systemd/system/"
BOLD "systemctl enable ${SRV_NAME}"
BOLD "systemctl start ${SRV_NAME}"
}
## Show help
show_help() {
echo "usage: ./${PROJECT_NAME} [-h] -b bin_file -p parameter"
echo ' -b : set bin file name'
echo ' -p : set bin parameter'
echo ' -h, --help : Show help'
echo ''
echo '> bash <(curl -fsSL git.io/csh-buildctrl.sh)'
exit 0
}
# init args
init_args() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-b) BIN_FILE="$2"; shift ;;
-p) BIN_PARAM="$2"; shift ;;
-s) SRV_NAME="$2"; shift ;;
-h | --help) show_help ;;
*) ERRO "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
}
main() {
init_args "$@"
build_control
build_service
}
main "$@" || exit 1