ubuntu16.04上系统管理服务和配置

1. service方式

service其实管理的是脚本,有一些需要我们自己编写,脚本位置在/etc/inin.d/

root@ubuntu:/# cd /etc/init.d/
root@ubuntu:/etc/init.d# ls
acpid         checkfs.sh              docker          kmod                   networking       rc.local    single             umountroot
alsa-utils    checkroot-bootclean.sh  grub-common     lightdm                network-manager  rcS         skeleton           unattended-upgrades
anacron       checkroot.sh            halt            mountall-bootclean.sh  ondemand         README      speech-dispatcher  urandom
apparmor      console-setup           hostname.sh     mountall.sh            open-vm-tools    reboot      supervisor         uuidd
apport        cron                    hwclock.sh      mountdevsubfs.sh       plymouth         resolvconf  thermald           whoopsie
avahi-daemon  cups                    irqbalance      mountkernfs.sh         plymouth-log     rsync       udev               x11-common
bluetooth     cups-browsed            kerneloops      mountnfs-bootclean.sh  pppd-dns         rsyslog     ufw
bootmisc.sh   dbus                    keyboard-setup  mountnfs.sh            procps           saned       umountfs
brltty        dns-clean               killprocs       mysql                  rc               sendsigs    umountnfs.sh

 

例如:我们想查看 docker的状态,service docker status

需求:在桌面写一个test_service.py,让 service test_service start /service test_service stop / service test_service restart / service test_service status 都可以执行

 

 

2. systemctl方式

这种方式管理的是服务xxx.service,读取服务配置,/lib/systemd/system

 

练习一:自己写一个xx.py,被systemctl管理

test.py

import random
import time
a = random.randint(1,10)
while 1:
    time.sleep(1)
    print(a)


if __name__ == "__main__":
    pass

 

test.sh

#! /bin/bash
# Provides:          Python_Test
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Python Test

file_path="/home/yanyanzhang/Desktop"
echo $file_path
python3.7 $file_path/test.py

#####
上面的Default-Start必须写,要不这一步就会报如下的错。

root@ubuntu:/lib/systemd/system# systemctl enable python_test
Synchronizing state of python_test.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable python_test
insserv: warning: script 'K01python_test' missing LSB tags and overrides
insserv: warning: script 'python_test' missing LSB tags and overrides
update-rc.d: error: python_test Default-Start contains no runlevels, aborting.  # 运行等级

 

python_test.service

[Unit]
Description=PythonTest

[Service]
Type=simple
Restart=on-failure
ExecStart=/bin/sh /home/yanyanzhang/Desktop/test.sh
KillSignal=SIGIN



[Install]
WantedBy=multi-user.target

 

systemctl enable python_test

systemctl start python_test

systemctl status python_test

root@ubuntu:/lib/systemd/system# systemctl status python_test
● python_test.service - PythonTest
   Loaded: loaded (/lib/systemd/system/python_test.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-05-01 19:25:05 CST; 2s ago
 Main PID: 128376 (sh)
    Tasks: 2
   Memory: 4.3M
      CPU: 26ms
   CGroup: /system.slice/python_test.service
           ├─128376 /bin/sh /home/yanyanzhang/Desktop/test.sh
           └─128377 python3.7 /home/yanyanzhang/Desktop/test.py

May 01 19:25:05 ubuntu systemd[1]: Started PythonTest.
May 01 19:25:05 ubuntu sh[128376]: /home/yanyanzhang/Desktop

 

停止  systemctl stop python_test

root@ubuntu:/lib/systemd/system# systemctl stop python_test
root@ubuntu:/lib/systemd/system# systemctl status python_test
● python_test.service - PythonTest
   Loaded: loaded (/lib/systemd/system/python_test.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sat 2021-05-01 19:38:21 CST; 15s ago
  Process: 128376 ExecStart=/bin/sh /home/yanyanzhang/Desktop/test.sh (code=killed, signal=TERM)
 Main PID: 128376 (code=killed, signal=TERM)

May 01 19:25:05 ubuntu systemd[1]: Started PythonTest.
May 01 19:25:05 ubuntu sh[128376]: /home/yanyanzhang/Desktop
May 01 19:38:21 ubuntu systemd[1]: Stopping PythonTest...
May 01 19:38:21 ubuntu systemd[1]: Stopped PythonTest.

 

练习二:不使用test.sh,直接运行test.py

python_test_two.service  /lib/systemd/system/

root@ubuntu:/lib/systemd/system# cat python_test_two.service 
[Unit]
Description=PythonTestTwo

[Service]
Type=simple
Restart=on-failure
ExecStart=/usr/local/bin/python3.7 /home/yanyanzhang/Desktop/test.py
KillSignal=SIGN

[Install]
WantedBy=multi-user.target

 

加载服务,启动服务

root@ubuntu:/lib/systemd/system# systemctl enable python_test_two.service  
Created symlink from /etc/systemd/system/multi-user.target.wants/python_test_two.service to /lib/systemd/system/python_test_two.service.
root@ubuntu:/lib/systemd/system# systemctl start python_test_two
root@ubuntu:/lib/systemd/system# systemctl status python_test_two
● python_test_two.service - PythonTestTwo
   Loaded: loaded (/lib/systemd/system/python_test_two.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-05-01 21:00:27 CST; 7s ago
 Main PID: 129073 (python3.7)
    Tasks: 1
   Memory: 3.1M
      CPU: 17ms
   CGroup: /system.slice/python_test_two.service
           └─129073 /usr/local/bin/python3.7 /home/yanyanzhang/Desktop/test.py

May 01 21:00:27 ubuntu systemd[1]: Started PythonTestTwo.

 

systemctl有一个缺点,而且 systemctl start xxx.service/systemctl stop xxx.service 启动和停止服务,都没有任何提示,还非得要 systemctl status xxx.service 来查看服务的运行状态,非常不方便。

systemctl 不加.service也可以启动,重启,停止

 

systemctl enable name.service 启用开机自启动服务

systemctl disable name.service 停止开启自启动服务

 

systemctl is-enabled name.service 查看服务是否为开机自启动

root@ubuntu:/# systemctl is-enabled python_test_two.service
enabled

 

systemctl try-restart name.service 只重启运行中的服务

systemctl --failed 查看启动失败的服务列表

systemctl --all 查看所以服务 空格翻页,q退出

systemctl list-dependencies --after name.service 列出指定服务之前启动的服务,依赖,空格翻页,q退出

systemctl list-dependencies --before name.service 列出指定服务之后启动的服务,被依赖,空格翻页,q退出

 

下面来看看,.service各部分配置的含义

1. Unit : 启动顺序和依赖关系

Description字段:给出当前服务的简单描述。
Description=Python_Test_Two

Documentation字段:给出文档位置。
After字段:表示本服务应该在某服务之后启动。
Before字段:表示本服务应该在某服务之前启动。
After和Before字段只涉及启动顺序,不涉及依赖关系。设置依赖关系,需要使用Wants字段和Requires字段。
Wants字段:表示本服务与某服务之间存在“依赖”系,如果被依赖的服务启动失败或停止运行,不影响本服务的继续运行。
Requires字段,表示本服务与某服务之间存在“强依赖”系,如果被依赖的服务启动失败或停止运行,本服务也必须退出。

2. Service : 定义如何启动/重启/停止服务。


2.1 Type字段定义启动类型。它可以设置的值如下。

simple(默认值):ExecStart字段启动的进程为主进程。

forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程。

oneshot:类似于simple,但只执行一次,Systemd会等它执行完,才启动其他服务。

dbus:类似于simple,但会等待D-Bus信号后启动。

notify:类似于simple,启动结束后会发出通知信号,然后Systemd再启动其他服务。

idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。

2.2 ExecStart字段-启动服务
启动服务时执行的命令,可以是可执行程序、系统命令或shell脚本。

2.3 ExecReload字段-重启服务
重启服务时执行的命令,可以是可执行程序、系统命令或shell脚本。

2.4 ExecStop字段-停止服务
停止服务时执行的命令,可以是可执行程序、系统命令或shell脚本。

服务配置文件还可以读取环境变量参数文件


3. Install :定义如何安装这个配置文件,即怎样做到开机启动。

WantedBy=multi-user.target
WantedBy字段:表示该服务所在的Target。

Target的含义是服务组,表示一组服务。WantedBy=multi-user.target指的是,Python_Test_Two所在的Target是multi-user.target(多用户模式)。

这个设置非常重要,因为执行systemctl enable python_test_two.service命令时,python_test_two.service会被链接到/etc/systemd/system/multi-user.target.wants目录之中,实现开机启动的功能。

 

 

 

 

# TODO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

# TODO

posted @ 2021-05-01 17:02  张京墨  阅读(914)  评论(0编辑  收藏  举报