linux服务检查进程

启动进程

检测脚本

server.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
#/*
# * File: .sh
# * Author: 82488059@qq.com
# * server.sh
# */
source /etc/profile
#
params()
{
return $#
}

while true
do
wid=`pidof transmission-daemon`
params ${wid}
num=$?
if [ "0" == "${num}" ]; then
# 启动进程
echo "transmission-daemon -g /var/lib/transmission/info -e /tmp/transmission.log &"
transmission-daemon -g /var/lib/transmission/info -e /tmp/transmission.log
elif [ "1" == "${num}" ]; then
# 只有一个进程
echo "transmission-daemon is running"
else
# 启多了就kill
killall transmission-daemon
fi
sleep 5s
done

注册服务

/etc/systemd/system/server.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#/*
# * File: .service
# * Author: 82488059@qq.com
# * Atuo generate by tciserverconf.sh
# */
[Unit]
Description=server
After=network.target

[Service]
WorkingDirectory=/
ExecStart=/usr/bin/bash /server.sh
Standardoutput=syslog
standardError=inherit
User=root
Group=root
Environment="HOME_PATH=/home/path"

[Install]
WantedBy=multi-user.target
EOF