CentOS7.5下开发systemctl管理的自定义Rsync启动服务程序

1.1 systemctl知识简介

从CentOS7 Linux开始,系统里的网络服务启动已经从传统的service改成了systemctl(一个systemd工具,主要负责控制systemd系统和服务管理器。),管理开机自启动的命令也从chkconfig改为了systemctl,由systemctl一个命令代替了CentOS7以前系统中的service和chkconfig两个命令。

系统服务的脚本也从传统的路径的/etc/init.d(/etc/rc.d/init.d/),改到了/usr/lib/systemd(除此之外还有/etc/systemd/system),需要自启动运行的程序,一般存在这个系统服务目录下,即:/usr/lib/systemd/system目录,每一个服务以“服务名.service”结尾,该文件的内容一般分为3部分:即[Unit]、[Service]和[Install],

1.2 systemctl管理的sshd服务配置介绍

下面是系统中sshd服务配置及解释说明。

[root@oldboy ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]   #<==对该系统服务描述及说明模块。
Description=OpenSSH server daemon            #<==描述性说明。
Documentation=man:sshd(8) man:sshd_config(5) #<==文档列表说明。
After=network.target sshd-keygen.service     #<==服务依赖类别说明。
Wants=sshd-keygen.service     #<==可选的依赖服务。

[Service]   #<==系统服务的运行参数设置模块
Type=notify #<==服务类型,可选有forking、notify、simple等。
EnvironmentFile=/etc/sysconfig/sshd  #<==环境变量等的配置文件。
ExecStart=/usr/sbin/sshd -D $OPTIONS #<==服务的启动程序。
ExecReload=/bin/kill -HUP $MAINPID   #<==重启程序。
KillMode=process
Restart=on-failure
RestartSec=42s

[Install] #<==服务安装的相关设置。
WantedBy=multi-user.target   #<==这里为设置多用户级别。可为空格分隔的列表, 表示在使用 systemctl enable 启用此单元时, 将会在对应的目录设置对应文件的软连接。
更多说明,可参考systemd.unit、system.service文档,此不细述,都掌握了意义也不大,可以写出启动脚本即可。

1.3 systemctl管理的rsyncd_zhangweibin服务配置说明

下面是我们人为配置的rsyncd服务配置及详细解释说明,因为系统里已经配置好了rsyncd.service,因此这里使用rsyncd_zhangweibin.service以区别。

[root@zhangweibin ~]# cat /usr/lib/systemd/system/rsyncd_zhangweibin.service
[Unit]
Description=Rsync service
After=network.target

[Service]
Type=forking  #<==后台运行。
PIDFile=/var/run/rsyncd.pid                #<==PID路径。
ExecStart=/etc/rc.d/init.d/rsyncd start    #<==兼容CentOS6的启动服务脚本,介绍在下文。
ExecReload=/etc/rc.d/init.d/rsyncd restart #<==兼容CentOS6的重启服务脚本,介绍在下文。
ExecStop=/etc/rc.d/init.d/rsyncd stop      #<==兼容CentOS6的停止服务脚本,介绍在下文。
PrivateTmp=true    #<==开启独立的空间。

[Install]
WantedBy=multi-user.target  #<==这里为设置多用户级别。
特别说明:本文仅仅使用rsync服务为例进行示例,对于系统没有的服务可以这样完成利用systemctl管理,但其实rsync服务系统已经早就配置好了,很简单简洁。
[root@oldboy ~]# cat /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"

[Install]
WantedBy=multi-user.target

1.4 Centos6下的rsync专业启动脚本

下面是Centos6下的rsync专业启动脚本,在/usr/lib/systemd/system/rsyncd_zhangweibin.service里面会使用这个脚本。

[root@zhangweibin ~]# cat /etc/rc.d/init.d/rsyncd
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: zhangweibin
[ -f /etc/rc.d/init.d/functions ] && source /etc/rc.d/init.d/functions
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/rsync"
rsyncd_pid_file_path="/var/run/rsyncd.pid"
#成功提示函数
log_success_msg(){
    #action为特殊的提示函数,$@为所有参数。
    action "SUCCESS! $@" /bin/true
}
#失败提示函数
log_failure_msg(){
    action  "ERROR! $@" /bin/false
}
start(){
    rsync --daemon &>/dev/null
    retval=$?
    if [ $retval -eq 0 ]
    then
        log_success_msg "Rsyncd is started."
        if test -w "$lockdir" #判断锁目录是否可写。
        then
            touch "$lock_file_path" #创建锁文件。
            return $retval
        else
            log_failure_msg "Rsync lockfile denied" #调用失败函数提示。
            return 1
        fi
    else
        echo "Rsyncd startup fail."
        return 1
    fi
}
stop(){
    if test -s "$rsyncd_pid_file_path"
    then
        #读取pidfile
        rsyncd_pid=`cat "$rsyncd_pid_file_path"`

        if (kill -0 $rsyncd_pid 2>/dev/null)
        then
            kill $rsyncd_pid
            retval=$?
            if [ $retval -eq 0 ]
            then
                log_success_msg "Rsync Stop" #调用停止成功函数。
                if test -f "$lock_file_path"
                then
                    rm "$lock_file_path"  #删除锁文件。
                fi
                return $retval
            else
                log_failure_msg "Rsyncd Stop."
                return $retval
            fi
        else
            log_failure_msg "rsync server_pid's process is not running!"
            rm "$rsyncd_pid_file_path"
        fi
    else
        log_failure_msg "Rsync server PID file is null or not exist!"
        return 1
    fi
}
case "$1" in
    start)
        start
        retval=$?
        ;;
    stop)
        stop
        retval=$?
        ;;
    restart)
        stop
        sleep 2
        start
        retval=$?
        ;;
    *)
        echo $"Usage:$0 {start|stop|restart}"
        exit 1
esac
exit $retval

1.5 配置开机自启动以及实现systemctl管理rsync服务

1、设置开机自启动

[root@oldboy ~]# systemctl enable rsyncd_zhangweibin.service
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd_zhangweibin.service to /usr/lib/systemd/system/rsyncd_zhangweibin.service.

2、查看设置状态

[root@zhangweibin ~]# systemctl status rsyncd_zhangweibin.service
rsyncd_zhangweibin.service - Rsync service
   Loaded: loaded (/usr/lib/systemd/system/rsyncd_zhangweibin.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
[root@zhangweibin ~]# systemctl is-enabled rsyncd_zhangweibin.service
enabled

3、使用systemctl启动rsync并查看状态

[root@zhangweibin ~]# systemctl start rsyncd_zhangweibin.service
[root@zhangweibin ~]# systemctl status rsyncd_zhangweibin.service
● rsyncd_zhangweibin.service - Rsync service
   Loaded: loaded (/usr/lib/systemd/system/rsyncd_zhangweibin.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2018-08-07 18:56:06 CST; 23s ago
  Process: 1586 ExecStart=/etc/rc.d/init.d/rsyncd start (code=exited, status=0/SUCCESS)
 Main PID: 1590 (rsync)
   CGroup: /system.slice/rsyncd_zhangweibin.service
           └─1590 rsync --daemon
8月 07 18:56:06 zhangweibin systemd[1]: Starting Rsync service...
8月 07 18:56:06 zhangweibin rsyncd[1590]: rsyncd version 3.1.2 starting, listening on port 873
8月 07 18:56:06 zhangweibin rsyncd[1586]: SUCCESS! Rsyncd is started. [  确定  ]
8月 07 18:56:06 zhangweibin systemd[1]: Started Rsync service.

4、使用lsof检查真实服务状态

[root@zhangweibin ~]# lsof -i :873 #<==需要yum install lsof -y。
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   1590 root    4u  IPv4  25765      0t0  TCP *:rsync (LISTEN)
rsync   1590 root    5u  IPv6  25766      0t0  TCP *:rsync (LISTEN)

5、使用systemctl停止rsync并查看状态

[root@zhangweibin ~]# systemctl stop rsyncd_zhangweibin.service
[root@zhangweibin ~]# lsof -i :873

6、使用systemctl取消rsync开机自启动并查看状态

[root@zhangweibin ~]# systemctl disable rsyncd_zhangweibin
Removed symlink /etc/systemd/system/multi-user.target.wants/rsyncd_zhangweibin.service.
[root@zhangweibin ~]# systemctl is-enabled rsyncd
disabled

通过以上的实践,我们看到已经可以使用systemctl替代了传统的service和chkconfig,如果你看懂了,就赶紧实践吧。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容