当前位置: 首页 > Linux

Systemctl命令介绍及使用

时间:2023-04-06 19:57:24 Linux

Systemd程序Systemd其实是Linux系统用来管理系统的一个程序,用来代替原来的init进程(用来管理和启动系统的其他服务进程),现在很多Linux发行版自动带有Systemd程序。systemctl命令1.Unitsystemctl命令是Systemd中最重要的命令。它用于启动和停止服务。Systemd中有一个Unit的概念。每个进程都是一个Unit,一共有十二种Unit。Serviceunit,系统服务Target单元,由多个单元组成的一组DeviceUnit,硬件设备MountUnit,文件系统挂载点AutomountUnit,自动挂载点PathUnit,文件或路径ScopeUnit,不被Systemd启动的ExternalprocessSliceUnit,进程组SnapshotUnit,Systemdsnapshot,可以切换回一个snapshotSocketUnit,socketSwap进程间通信的Unit,swapfileTimerUnit,timer2.常用命令#列出运行中的Unitsystemctllist-units,可以直接用systemctl#列出所有单元,包括systemctllist-units--all#找不到配置文件或启动失败systemctllist-units--all#列出所有未运行的单元systemctllist-units--all--state=inactive#listAllfailedloadingUnitssystemctllist-units--failed#列出所有正在运行的Unitsystemctllist-units--type=service#显示某个Unit是否在运行systemctlis-activeapplication.service#显示某个Unit是否在启动失败状态systemctlis-failedapplication.service#显示Unit服务是否建立启动链接systemctlis-enabledapplication.service#立即启动服务sudosystemctlstartapache.service#立即停止服务sudosystemctlstopapache。service#重启一个服务sudosystemctlrestartapache.service#重新加载一个服务的配置文件sudosystemctlreloadapache.service#重新加载所有修改过的配置文件sudosystemctldaemon-reload上面说的systemctl中Unit的配置文件每个服务都是一个Unit,每个Unit都会有它的配置文件,这样一启动就知道如何启动Systemd,默认从目录/etc/systemd/system/读取配置文件。但是里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录下。1、查看Unit的配置文件,可以使用```systemctlcat```命令查看服务的配置文件。下面是Mysql的配置文件。很多软件已经支持Systemd程序,其Unit配置会在安装文件时自动配置,例如Mysql、Nginx等。```[root@VM_0_11_centos~]#systemctlcatmysqld#/usr/lib/systemd/system/mysqld.service[Unit]Description=MySQLServerDocumentation=man:mysqld(8)Documentation=http://dev.mysql。com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]User=mysqlGroup=mysqlType=forkingPIDFile=/var/run/mysqld/mysqld.pid#禁用systemd对mysqld服务的服务启动和停止超时逻辑。TimeoutSec=0#以root身份执行pre和post脚本PermissionsStartOnly=true#需要创建系统表ExecStartPre=/usr/bin/mysqld_pre_systemd#启动mainserviceExecStart=/usr/sbin/mysqld--daemonize--pid-file=/var/run/mysqld/mysqld.pid$MYSQLD_OPTS#使用这个来切换malloc实现EnvironmentFile=-/etc/sysconfig/mysql#设置open_files_limitLimitNOFILE=5000Restart=on-failureRestartPreventExitStatus=1PrivateTmp=假```2。Unit配置文件的含义可以看到Unit配置文件有很多tag,不同的tag代表不同的含义。这里只做一些介绍。可以去官网查看Unit配置文件文档,https://www.freedesktop.org/software/systemd/man/systemd.unit.html-Unit-Description,servicedescription-Documentation,文档介绍-after,服务启动后要启动,例如mysql需要在network和syslog启动后,开始-安装-WantedBy,值为一个或多个Targets。当当前Unit被激活(enable)时,符号链接会放在/etc/systemd/system目录下Target名称+.wants后缀组成的子目录中-RequiredBy目录中,其值为一个或多个Targets,当当前Unit被激活(enable)时,符号链接会放在/etc/systemd/system目录下Target名称+.required后缀组成的子目录-Alias,当前Unit可以使用的别名start-另外,当当前Unit被激活(enable)时,其他会同时被激活的Unit-Service-Type,定义了启动时的流程行为。它具有以下值。-Type=simple,默认值,执行ExecStart指定的命令,启动主进程-Type=forking,从父进程以fork的形式创建子进程,创建后父进程会立即退出-Type=oneshot,一次性进程,Systemd会等待当前服务退出,然后继续执行-Type=dbus,当前服务通过D-Bus启动-Type=notify,当前服务启动,会通知Systemd,然后继续执行-Type=idle,如果有其他任务执行完成后,会运行当前服务-ExecStart,启动当前服务的命令-ExecStartPre,启动当前服务前要执行的命令-ExecStartPost,启动当前服务后执行的命令-ExecReload,重启当前服务时执行的命令-ExecStop,停止当前服务执行时执行的命令-ExecStopPost,停止其服务后执行的命令-RestartSec,当前服务自动重启之间的秒数-Restart,定义在什么情况下Systemd会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal,on-abort,on-watchdog-TimeoutSec,定义Systemd停止当前服务前等待的秒数-Environment,指定环境变量自定义服务启动由于Systemd的作用是控制服务的启动,然后就可以添加自己的服务了,可以直接使用systemctl命令来控制服务的启动,也可以设置开机自动启动等。1.创建一个Unit配置文件在`??``/创建自己的配置文件usr/lib/systemd/system```目录,一般以```.service```结尾,比如在这里创建一个```test-sh.service```配置文件,这个Unit就是启动一个我们自己的shell脚本。```#/usr/lib/systemd/system/test-sh.service[Unit]Description=testshlog[Service]ExecStart=/opt/dev/shell/test.shType=forkingKillMode=processRestart=on-failureRestartSec=30s[安装]WantedBy=多用户.target```2。创建脚本在上面配置文件中指定的启动路径```/opt/dev/shell/```下创建一个shell脚本,这里只是打印当前时间,输出为文本。```#!/bin/bashwhiletruedosleep1date=`date-dtoday+"%Y-%m-%d%T"`echo${date}>>/opt/dev/shell/test.txtdone```3。加载配置文件并开始使用```systemctldaemon-reload```命令加载新添加的配置文件,然后使用```systemctlstarttest-sh.service```命令启动,然后使用```systemctlstatustest-sh.service```命令查看状态,可以看到已经启动了,```/opt/dev/shell/test.txt```确实是不断写内容,最后用```systemctlstoptest-sh.service```命令停止服务,可以看到状态也停止了。注意修改配置文件后,必须使用```systemctldaemon-reload```命令加载新添加的配置文件,然后启动服务。```[root@VM_0_11_centos~]#systemctlstarttest-sh.service^C[root@VM_0_11_centos~]#systemctlstatustest-sh.service●test-sh.service-testshlogLoaded:loaded(/usr/lib/systemd/system/test-sh.service;已启用;供应商预设:已禁用)活动:自周五2020-06-2605:46:45CST起激活(启动);11秒前Control:9295(test.sh)CGroup:/system.slice/test-sh.service├─9295/bin/bash/opt/dev/shell/test.sh└─9343sleep1Jun2605:46:45VM_0_11_centossystemd[1]:Startingtestshlog...[root@VM_0_11_centos~]#systemctlstoptest-sh.service[root@VM_0_11_centos~]#systemctlstatustest-sh.service●test-sh.service-testsh日志已加载:已加载(/usr/lib/systemd/system/test-sh.service;已启用;供应商预设:已禁用)活动:自2020-06-26星期五05:47:52CST起处于非活动状态(死);2s前进程:9295ExecStart=/opt/dev/shell/test.sh(code=killed,signal=TERM)Jun2605:46:45VM_0_11_centossystemd[1]:Startingtestshlog...Jun2605:47:52VM_0_11_centossystemd[1]:Stoppedtestshlog.```查看Unit启动日志Systemd统一管理所有Unit的启动日志,所以只需要使用journalctl命令查看服务日志#查看所有日志(默认只保存本次启动的日志)journalctl#查看指定时间的日志journalctl--since="2012-10-3018:17:16"journalctl--since"20分钟前"journalctl---sinceyesterdayjournalctl--since"2015-01-10"--until"2015-01-1103:00"journalctl--since09:00--until"1hourago"#Displaythelatest10linesofjournalattheendofjournalctl-n#在末尾显示指定行数的日志journalctl-n20#滚动实时显示最新日志journalctl-f#查看指定服务的日志journalctl/usr/lib/systemd/systemd#查看指定进程的日志journalctl_PID=1#查看某个Script的日志journalctl/usr/bin/bash#查看指定用户的日志journalctl_UID=33--sincetoday#查看一个Unit的日志journalctl-unginx.servicejournalctl-unginx.service--sincetoday#滚动实时显示一个Unit的最新日志journalctl-unginx.service-f#合并显示多个的日志单位$journalctl-unginx.service-uphp-fpm.service--sincetoday