Understanding and Using systemctl command

A, systemctl introduce
       usual, Linux startup has been using the init process.

       We often see

$ sudo /etc/init.d/apache2 start

or

$ service apache2 start

 

       This approach has two drawbacks:

       First, the long start-up time. The init process is the serial started, only the first to start a process completed, will start the next process.

       Second, the startup script complicated. init process just execute the startup script, regardless of other things. The script needs its own deal with various situations, which often makes the script becomes very long.

       To this end, LINXU design introduced Systemd

       Its design goal is to provide a complete solution for start-up and management of the system. According to Linux convention, letter d is an abbreviation daemon (daemon) is. Systemd meaning of the name, it is to be the guardian of the entire system. Use the Systemd, then you do not need the init. Systemd replaced initd, became the first system of process (PID = 1), other processes are its children.

 

       By command on the chart, we can see Systemd version.

       In general, the advantages of Systemd is powerful, easy to use, the disadvantage is a huge system, very complicated. In fact, there are still a lot of people oppose the use of Systemd, the reason is that it is too complicated, with strong coupling to other parts of the operating system, in violation of "keep simple, keep stupid" of the Unix philosophy.

         

        The figure is Systemd architecture diagram, looking a bit scary!

 

Two, systemctl command
       Systemd is not a command, but a set of commands, involving all aspects of system administration.
       Here I introduce some commonly used commands

# Start redis service
$ sudo systemctl Start redis

# redis service restart
$ sudo systemctl restart redis

# redis View service status
$ sudo systemctl Status redis

# reboot the system
$ sudo reboot systemctl

# shut down the system, cut off the power supply
$ sudo systemctl poweroff
 

Third, the use systemctl add custom services
       in the daily development, we inevitably build a wide variety of services, then we can use systemctl add the service we need.

        In my system, for example, you can configure the service to a directory on the figure, system storage service system configuration, user directory service user to store custom configurations, of course, you can also put the system into service

 

          The figure is already setting up of ECF service, Unit, Service, Install three key words are

[Unit]
Description : 服务的简单描述
Documentation :服务文档
Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。
Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。
Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。
[Service]
Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。
Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。
PIDFile:pid文件路径
ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。
ExecReload:指定单元停止时执行的命令或者脚本。
ExecStop:指定单元停止时执行的命令或者脚本。
PrivateTmp:True表示给服务分配独立的临时空间
Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。
RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的值为假,这个选项只有在Type=oneshot时需要被配置。
[Install]
Alias:为单元提供一个空间分离的附加名字。
RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。
WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。
Also:指出和单元一起安装或者被协助的单元。
DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。

       好了,上面就是systemctl命令的概述,大家可以动手实践一下。

四、总结
       通过systemctl命令,我们对自己创建的服务进行自定义添加和使用,对于现在主流的微服务架构来说,这个命令带给我们很多便利的地方!

Guess you like

Origin www.cnblogs.com/eflypro/p/12144198.html