利用systemd作守护进程的脚本

#!/bin/sh
 
path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
mode=$1
name=receipt
systemd_dir=/usr/lib/systemd/system
app_process=`ps -ef | grep "$program"| grep -v grep`
force=$2

case "$mode" in
   'install')
        if [ -d $systemd_dir ];then
           if [ -f $systemd_dir/${name}.service ] && [ "$force" != "--force" ]; then
              echo "The ${name}.service had exist.!!!"
              echo "Force to install by command: install --force"
              exit
           fi
           echo "Description=$name service" > $systemd_dir/${name}.service
           echo "After=network.target" >> $systemd_dir/${name}.service
           echo "" >> $systemd_dir/${name}.service
           echo "[Service]" >> $systemd_dir/${name}.service
           echo "ExecStart=$path_script/$name --config=$path_script/config/config.toml" >> $systemd_dir/${name}.service
           echo "KillMode=process" >> $systemd_dir/${name}.service
           echo "Restart=on-failure" >> $systemd_dir/${name}.service
           echo "RestartSec=3s" >> $systemd_dir/${name}.service
           echo "" >> $systemd_dir/${name}.service
           echo "[Install]" >> $systemd_dir/${name}.service
           echo "WantedBy=multi-user.target" >> $systemd_dir/${name}.service
           systemctl daemon-reload
           systemctl enable $name
           systemctl start $name
        else
           echo "it's not support systemd feature"
        fi
        ;;
   'uninstall')
        if [ -f $systemd_dir/${name}.service ]; then
           systemctl stop $name
           systemctl disable $name
           rm -f /etc/systemd/system/multi-user.target.wants/${name}.service
           rm -f $systemd_dir/${name}.service
        fi
        ;;
   'migrate')
 	echo "it's ready to migrate database...."
	$path_script/$name --config=$path_script/config/config.toml --migrate=true
	;;
   'start')
        echo "it's ready to start op...."
        systemctl start $name
        systemctl status $name
        echo "start end......"
        ;;
   'stop')
        echo "it's ready to check process..."
        systemctl stop $name
        systemctl status $name
        echo 'success to kill.'
        ;;
   'status')
        echo "it's ready to check status..."
        systemctl status $name
        ;;
    *)
        basename=`basename "$0"`
        echo "Usage: $basename  {install|uninstall|start|stop}  [ server options ]"
        exit 1
        ;;
esac
exit 1