CentOS6 & CentOS7服务启动文件

1.CentOS6

(1)以rsync为例

#!/bin/bash
# chkconfig: 2345 20 80
# description: rsync.sh
function start_rsync {
        if [ ! -s /var/run/rsyncd.pid ];then
                /usr/bin/rsync --daemon
        else
                echo -e "\033[33mRsync is already running\033[0m"
        fi
}

function stop_rsync {
        if [ -s /var/run/rsyncd.pid ];then
                kill `cat /var/run/rsyncd.pid`
        else
                echo -e "\033[33mRsync is no running\033[0m"
                return 1
        fi
}

function restart_rsync {
        stop_rsync
        if [ $? -ne 1 ];then
                sleep 1
                start_rsync
        fi
}
case $1 in
        start)
        start_rsync
        ;;
        stop)
        stop_rsync
        ;;
        restart)
        restart_rsync
        ;;
        *)
        echo -e "\033[33mUsage: start | stop | restart \033[0m"
        ;;
esac

(2)给执行权限

chmod +x /etc/init.d/rsync.sh

(3)加入系统服务和开机自启

chkconfig  --add rsync.sh
chkconfig rsync.sh on

2.CentOS7

(1)以rsync为例

[Unit]
After=network.target remote-fs.target

[Service]
Type=forking
ExecStart=/etc/init.d/rsync.sh start
ExecReload=/etc/init.d/rsync.sh restart
ExecStop=/etc/init.d/rsync.sh stop

[Install]
WantedBy=multi-user.target
posted @ 2020-01-14 19:03  IMSCZ  阅读(815)  评论(0编辑  收藏  举报