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
|
export TESTDIR=/var/work/
cat>${TESTDIR}/testserver.sh<<EOF # testserver source /etc/profile
echo "start testserver" > /tmp/testserver.log
while true do # 进程名称test wid=\`pidof test\` if [ "" == "\$wid" ]; then echo "${TESTDIR}/test &" >> /tmp/testserver.log ${TESTDIR}/test & else echo "testserver is running" >> /tmp/testserver.log fi sleep 5s done
EOF
cat>/etc/systemd/system/testserver.service<<EOF [Unit] Description=testserver After=network.target
[Service] WorkingDirectory=${TESTDIR} ExecStart=/usr/bin/bash ${TESTDIR}/testserver.sh Standardoutput=syslog standardError=inherit User=root Group=root Environment="HOME_PATH=/home/path"
[Install] WantedBy=multi-user.target EOF
systemctl enable testserver
systemctl start testserver
echo install testserver service done.
exit 0
|