linux系统服务的开启关闭和状态观察


systemctl单一服务(service unit)的管理

回到顶部
一般来说,服务的启动有两种方式,一种是系统开机的时候启动服务,另一个是系统开机后用户手动启动服务。
[root@initroot ~]# systemctl [command] [unit]
command选项:
start :立刻启动unit
stop :立刻关闭unit
restart :立刻关闭后再启动unit,即重启unit
reload :不关闭unit的情况下,重载配置文件,让设置生效
enable :设置开机默认启动unit
disable :设置开机默认不启动unit
status :unit的运行状态,会列出有没有正在运行、开机预设执行否、登录等状态信息等!
is-active :目前有没有正在运作中
is-enable :有没有开机默认启用unit
查看atd服务的状态:
[root@initroot ~]# systemctl status atd.service
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-10-30 21:35:36 CST; 4 months 2 days ago
 Main PID: 506 (atd)
   CGroup: /system.slice/atd.service
           └─506 /usr/sbin/atd -f

Oct 30 21:35:36 izm5e94e7bypr5rhnhbvq9z systemd[1]: Started Job spooling tools.
上面各行的含义如下:
Loaded:说明开机是否默认启动unit,enabled为开机启动,disabled为开机不启动
Active:unit的运行状态,为正在运行(running)或没有运行(dead)
后面两行说明unit的PID信息;
最后一行显示服务的日志文件信息!信息格式为:时间 讯息发送主机 哪一个服务的讯息 实际讯息内容
如果unit曾经出错过,观察这个地方也是相当重要的!
可以看到atd服务默认开机启动,目前正在运行!
关闭atd服务:
[root@initroot ~]# systemctl stop atd.service
            
再次查看atd服务的状态:
[root@initroot ~]# systemctl status atd.service
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2020-03-03 13:54:46 CST; 8s ago
  Process: 506 ExecStart=/usr/sbin/atd -f $OPTS (code=exited, status=0/SUCCESS)
 Main PID: 506 (code=exited, status=0/SUCCESS)

Oct 30 21:35:36 izm5e94e7bypr5rhnhbvq9z systemd[1]: Started Job spooling tools.
Mar 03 13:54:46 izm5e94e7bypr5rhnhbvq9z systemd[1]: Stopping Job spooling tools...
Mar 03 13:54:46 izm5e94e7bypr5rhnhbvq9z systemd[1]: Stopped Job spooling tools.
unit运行状态为inactive (dead),没有运行。开机默认启动,下次开机还是会启动。
最后两行为新增加的日志讯息,告诉我们目前的系统状态喔!
现在再次启动atd服务:
              [root@initroot ~]# systemctl start atd.service
            
系统服务进程的管理一定要使用systemctl,尽量不要使用kill来关闭服务进程!
这样会使服务进程脱离systemd的监控,后续就无法使用systemctl命令来管理了。
服务的运行状态除了上面的running和dead之外,基本上有如下几个常见的状态:
systemd服务运行状态
systemd服务运行状态
systemd服务运行状态 状态说明
active (running) 正有一只或多只程序正在系统中执行的意思,举例来说,正在执行中的vsftpd就是这种模式。
active (exited) 仅执行一次就正常结束的服务,目前并没有任何程序在系统中执行。 举例来说,开机或者是挂载时才会进行一次的quotaon功能,就是这种模式! quotaon不须一直执行~只须执行一次之后,就交给文件系统去自行处理啰!通常用bash shell写的小型服务,大多是属于这种类型(无须常驻内存)。
active (waiting) 正在执行当中,不过还再等待其他的事件才能继续处理。举例来说,打印的队列相关服务就是这种状态! 虽然正在启动中,不过,也需要真的有队列进来 (打印作业) 这样他才会继续唤醒打印机服务来进行下一步打印的功能。
inactive 这个服务目前没有运作。
系统服务的开机默认状态除了enable和disable之外,还有如下几种状态:
systemd服务默认启动状态
systemd服务默认启动状态
systemd服务默认启动状态 状态说明
enabled 开机时启动
disabled 开机时不启动
static 不可以自己启动(enable不可),不过可能会被其他的enabled的服务来唤醒(相依属性的服务)
mask 这个daemon无论如何都无法被启动!因为已经被强制注销(非删除)。可通过systemctl unmask方式改回原本状态
找到系统中名为chronyd的服务,观察此服务的状态,观察完毕后,将此服务设置为: 1)开机不会启动 2)现在状况是关闭的情况!
1. 观察chronyd服务的状态,确认是否为关闭/未启动呢?
[root@initroot ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
2. 由上面知道目前是启动的,关闭该服务:
[root@initroot ~]# systemctl stop chronyd.service
3. 设置开机不启动:
[root@initroot ~]# systemctl disable chronyd.service
rm '/etc/systemd/system/multi-user.target.wants/chronyd.service'
在用disble设置开机默认不启动chronyd服务,可以看到其实就是/etc/systemd/system目录下的链接文件删除
[root@initroot ~]# systemctl status chronyd.service
chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled)
Active: inactive (dead)
chronyd这个服务完整的关闭了,目前没有运行,开机后也不会运行。
cups为打印服务进程
1. 查看cups服务的状态:
[root@initroot ~]# systemctl status cups.service
● cups.service - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-03 12:17:41 CST; 2h 12min ago
     Docs: man:cupsd(8)
 Main PID: 2487 (cupsd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/cups.service
           └─2487 /usr/sbin/cupsd -l

Mar 03 12:17:41 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
默认启动状态为enable但是当前运行状态却是inactive!
2. 关闭并设置开机不启动:
[root@initroot ~]# systemctl stop cups.service
[root@initroot ~]# sudo systemctl disable cups.service
Synchronizing state of cups.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable cups
Removed /etc/systemd/system/sockets.target.wants/cups.socket.
Removed /etc/systemd/system/multi-user.target.wants/cups.path.
Removed '/etc/systemd/system/printer.target.wants/cups.service'
上面取消了三个链接文件,这三个文件都是和cups.service服务相关的文件!
因为我们已经彻底关闭了cups.service,使用netstat是观察不到该服务的:
[root@initroot ~]# netstat -tlunp | grep cups
3. 尝试启动cups.socket监听客户端的需求!
[root@initroot ~]# systemctl start cups.socket
[root@initroot ~]# sudo systemctl status cups.service cups.socket cups.path
Failed to dump process list, ignoring: No such file or directory
● cups.service - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2020-03-03 14:31:07 CST; 2min 46s ago
     Docs: man:cupsd(8)
 Main PID: 2487 (code=exited, status=0/SUCCESS)

Mar 03 12:17:41 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
Mar 03 14:31:07 peter-VirtualBox systemd[1]: Stopping CUPS Scheduler...
Mar 03 14:31:07 peter-VirtualBox systemd[1]: Stopped CUPS Scheduler.

● cups.socket - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.socket; disabled; vendor preset: enabled)
   Active: active (listening) since Tue 2020-03-03 14:33:41 CST; 11s ago
   Listen: /run/cups/cups.sock (Stream)
   CGroup: /system.slice/cups.socket

Mar 03 14:33:41 peter-VirtualBox systemd[1]: Listening on CUPS Scheduler.

● cups.path - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.path; disabled; vendor preset: enabled)
   Active: inactive (dead)

Mar 03 12:12:39 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
Mar 03 12:17:41 peter-VirtualBox systemd[1]: Stopped CUPS Scheduler.
Mar 03 12:17:41 peter-VirtualBox systemd[1]: Stopping CUPS Scheduler.
Mar 03 12:17:41 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
Mar 03 14:31:07 peter-VirtualBox systemd[1]: Stopped CUPS Scheduler.
只有cups.socket启动了,其他服务都没有启动! 4. 尝试使用lp命令打印:
[root@initroot ~]# echo "testing" | lp
lp: Error - no default destination available. #没有打印机!错误也没关系!
            
再次观察cups.service服务状态:
[root@initroot ~]# systemctl status cups.service
● cups.service - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-03 14:35:11 CST; 28s ago
     Docs: man:cupsd(8)
 Main PID: 6213 (cupsd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/cups.service
           └─6213 /usr/sbin/cupsd -l

Mar 03 14:35:11 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
cups.service服务的状态竟然变成running了!
原来cups.service和cups.socket之间是有依赖关系的。 cups打印服务使用cups.socket文件监听port 631来提供网络打印机的打印功能。 但是其实不需要cups打印服务一直监听cups.socket文件,在用户有需要打印时,即cups.socket有数据时,才会主动唤醒cups.service! 因此,如果你仅是 disable/stop cups.service 而忘记了其他两个服务的话,那么当有用户向其他两个cups.path, cups.socket提出要求时, cups.service就会被唤醒!所以关掉也没用!
强迫服务注销 (mask) 的练习比较正规的作法是,要关闭 cups.service 时,连同其他两个会唤醒 service 的 cups.socket 与 cups.path 通通关闭,那就没事了!
比较不正规的作法是,那就强迫 cups.service 注销吧!通过 mask的方式来将这个服务注销看看!
1. 保持刚刚的状态,关闭cups.service,启动cups.socket,然后注销cups.servcie
[root@initroot ~]# systemctl stop cups.service
[root@initroot ~]# systemctl mask cups.service
ln -s '/dev/null' '/etc/systemd/system/cups.service'
其实mask只是cups服务的配置文件cups.service变成空设备/dev/null的符号链接!
[root@initroot ~]# systemctl status cups.service
● cups.service
   Loaded: masked (/dev/null; bad)
   Active: active (running) since Tue 2020-03-03 14:35:11 CST; 1min 32s ago
 Main PID: 6213 (cupsd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/cups.service
           └─6213 /usr/sbin/cupsd -l

Mar 03 14:35:11 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
Mar 03 14:36:11 peter-VirtualBox systemd[1]: cups.service: Current command vanished from the unit file, execution of the command list won't be resumed.
整个启动的配置文件被连结到/dev/null空设备文件,无论如何也无法启动cups.service 了! 通过这个 mask 功能,你就可以不必管其他相依服务可能会启动到这个想要关闭的服务了!虽然是非正规,不过很有效!
使用unmask取消注销:
[root@initroot ~]# systemctl unmask cups.service
rm '/etc/systemd/system/cups.service'
[root@initroot ~]# systemctl status cups.service
● cups.service - CUPS Scheduler
   Loaded: loaded (/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-03 14:35:11 CST; 2min 30s ago
     Docs: man:cupsd(8)
 Main PID: 6213 (cupsd)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/cups.service
           └─6213 /usr/sbin/cupsd -l

Mar 03 14:35:11 peter-VirtualBox systemd[1]: Started CUPS Scheduler.
Mar 03 14:36:11 peter-VirtualBox systemd[1]: cups.service: Current command vanished from the unit file, execution of the command list won't be resumed.
上面我们遇到了socket类型的unit,cups.socket。linux系统中还有很多socket文件,通过systemctl list-sockets可以列出系统中的正在被监听的socket file:
[root@initroot ~]# systemctl list-sockets
LISTEN                          UNIT                            ACTIVATES
/dev/rfkill                     systemd-rfkill.socket           systemd-rfkill.service
/run/acpid.socket               acpid.socket                    acpid.service
/run/avahi-daemon/socket        avahi-daemon.socket             avahi-daemon.service
/run/cups/cups.sock             cups.socket                     cups.service
/run/dmeventd-client            dm-event.socket                 dm-event.service
/run/dmeventd-server            dm-event.socket                 dm-event.service
/run/lvm/lvmetad.socket         lvm2-lvmetad.socket             lvm2-lvmetad.service
/run/lvm/lvmpolld.socket        lvm2-lvmpolld.socket            lvm2-lvmpolld.service
/run/systemd/coredump           systemd-coredump.socket        
/run/systemd/fsck.progress      systemd-fsckd.socket            systemd-fsckd.service
/run/systemd/initctl/fifo       systemd-initctl.socket          systemd-initctl.service
/run/systemd/journal/dev-log    systemd-journald-dev-log.socket systemd-journald.service
/run/systemd/journal/socket     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/stdout     systemd-journald.socket         systemd-journald.service
/run/systemd/journal/syslog     syslog.socket                   rsyslog.service
/run/udev/control               systemd-udevd-control.socket    systemd-udevd.service
/run/uuidd/request              uuidd.socket                    uuidd.service
/var/run/dbus/system_bus_socket dbus.socket                     dbus.service
audit 1                         systemd-journald-audit.socket   systemd-journald.service
kobject-uevent 1                systemd-udevd-kernel.socket     systemd-udevd.service

20 sockets listed.
Pass --all to see loaded but inactive sockets, too.
第一列为socket文件所在的目录,第二列为管理该socket文件的unit,第三列为依赖该socket文件的service。
可以看到cups.socket管理的socket文件为/run/cups/cups.sock,cups.service依赖该socket文件。

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读