CentOS6 & CentOS7服务启动文件

点点圈 提交于 2020-01-14 19:50:47

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!