我们的服务器基本都是CentOS7.x系统。通过系统源安装的服务一般可以通过systemctlstartXXX.service命令一键启动,但有些由于各种不可抗因素,只能通过解压tar包将组件安装到服务器上。每次启动都需要在组件目录下执行特定命令,管理起来很不方便。可以自己写systemctl配置文件,达到一键启停服务的效果。操作系统:CentOS7systemctl的配置脚本在服务器的/usr/lib/systemd目录下。system子目录包含系统服务,无需用户登录即可运行。用户子目录中的用户服务需要用户登录。可运行。其中.service为普通服务,.target为启动级服务。[root@v130systemd]#ll/usr/lib/systemd/systemtotal1328-rw-r--r--。1rootroot2752018年4月27日abrt-ccpp.service-rw-r--r--。1rootroot3802018年4月27日abrtd.service-rw-r--r--。1rootroot3612018年4月27日abrt-oops.service-rw-r--r--。pstoreoops.service-rw-r--r--。1rootroot262Apr272018abrt-vmcore.service......[root@v130systemd]#ll/usr/lib/systemd/usertotal24-rw-r--r--1rootroot131Nov32018at-spi-dbus-bus.service-rw-r--r--1rootroot457Mar182020basic.targetlrwxrwxrwx1rootroot26Apr1317:11bluetooth.target->../system/bluetooth.target-rw-r--r--1rootroot414Mar182020default.target-rw-r--r--1rootroot499Mar182020exit.target-rw-r--r--1rootroot147Nov32018glib-pacrunner.service……配置文件包含Unit(服务描述)、Install(安装配置)、Service(服务核心配置)三大模块。以mysqld.service服务为例,其官方配置脚本如下:##systemdservicefileforMySQLforkingserver#[Unit]Description=MySQLServer#ServiceDescriptionDocumentation=man:mysqld(8)#DocumentrelatedDocumentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.target#服务类,说明这个服务需要在网络服务启动后启动After=syslog.target[Install]WantedBy=multi-user.target#支持多用户[Service]User=mysql#服务运行用户组=mysql#服务运行用户组Type=forking#后台运行方式PIDFile=/var/run/mysqld/mysqld.pid#存储PID路径(绝对)#禁用systemd对mysqld的服务启停超时逻辑service.TimeoutSec=0#启停服务超时时间#以root身份执行pre和post脚本PermissionsStartOnly=true#是否使用root用户执行pre-postcommands#需要创建系统表ExecStartPre=/usr/bin/mysqld_pre_systemd#启动服务前执行的前置命令#startmainservice#启动服务的命令ExecStart=/usr/sbin/mysqld--daemonize--pid-file=/var/run/mysqld/mysqld.pid$MYSQLD_OPTS#使用这个切换malloc实现EnvironmentFile=-/etc/sysconfig/mysql#环境配置文件路径#setsopen_files_limitLimitNOFILE=5000Restart=on-failure#重启方式,异常退出时重启RestartPreventExitStatus=1PrivateTmp=false重点关注主要配置说明:[Service]Type后台运行模式simple默认值,立即启动,当前进程为主进程forking该服务将以fork()模式启动。启动后,可以关闭父进程,子进程作为守护进程运行。建议使用此方法。命令执行后oneshot将结束。notify执行完成后会发送通知信号,其他服务会继续启动dbus等待DBus信号后启动idle等待其他任务执行完毕再启动ExecStart服务启动命令ExecStartPre服务启动命令前ExecStartPost服务启动命令后ExecReload服务重启命令ExecStop服务停止命令ExecStopPost服务停止命令后PrivateTmp为服务分配独立的临时空间,True/FalsePIDFile指定存放进程PID的路径,用于跟踪进程号环境变量,用法:Environment=DATA_DIR=/opt/dataEnvironmentFile指定配置文件KillMode停止服务的方式control-group默认值,kill当前控制组内的所有进程process只kill主进程mixed向主进程发送sigterm终止信号,无条件发送sigkill给子进程的终止信号none不主动杀死任何进程,只执行ExecStop命令Restart自动重启服务no默认值,退出后不主动重启on-success正常退出后重启(退出状态码为0)on-failure异常退出后重启,包括信号终止和超时,daemons推荐这种方法on-异常信号终止或者Restarton-abortaftertimeout仅在没有接收到信号或信号终止后重启on-watchdog超时后始终重启任何退出都会重启RestartSec重启后等待的秒数[Install]WantedBymulti-user.target多用户命令行状态,推荐设置graphical.target图形界面,依赖multi-user.target[Unit]Before指定需要在某项服务启动前启动该服务After指定在启动该服务前需要启动某项服务注:exec***命令只接受指令Parameter参数..格式,不能接受<>|&等特殊字符,很多bash语法不支持。如果要支持bash语法,需要设置Type=oneshot。以Consul为例,创建consul.service配置:vi/usr/lib/systemd/system/consul.service[Unit]Description=ConsulServiceAfter=network.targetAfter=syslog.target[Service]User=consulType=simplePIDFile=/var/run/consul/consul.pidTimeoutSec=5ExecStart=/home/consulagent-server-ui-bootstrap-expect=1-data-dir=/consuldata-node=v130-bind=10.0.4.16-client=0.0.0.0-dns-port=8640-http-port=8530-serf-lan-port=8331-serf-wan-port=8332-server-port=8330Restart=on-failurePrivateTmp=true[Install]WantedBy=多用户。target配置完成后,给配置文件754以上权限,执行systemctldaemon-reload刷新系统配置[root@v130~]#chmod754/usr/lib/systemd/system/consul.service[root@v130~]#systemctldaemon-reload[root@v130~]#systemctlstartconsul[root@v130~]#systemctl状态consul●consul.service-领事服务已加载:已加载(/usr/lib/systemd/system/consul.service;已禁用;供应商预设:已禁用)活动:自2020年10月8日星期四19:38起处于活动状态(运行):19科技委;31s前MainPID:20455(consul)Tasks:22Memory:15.0MCGroup:/system.slice/consul.service└─20455/home/consulagent-server-ui-bootstrap-expect=1-data-dir=/consuldata-node=v130-bind=10.0.4.16-client=0.0.0.0-dns-port=8640...Oct0819:38:28v130consul[20455]:2020/10/0819:38:28[INFO]raft:Nodeat10.0.4.16:8330[Leader]enteringLeaderstateOct0819:38:28v130consul[20455]:2020/10/0819:38:28[INFO]consul:clusterleadershipacquiredOct0819:38:28v130consul[20455]:2020/10/0819:38:28[INFO]consul:Newleaderelected:v130Oct0819:38:48v130consul[20455]:==>NewerConsulversionavailable:1.8.4(currentlyrunning:1.4.3)如无意外,可以看到consul服务一直稳定运行,systemctlstatus命令可以查看状态service#systemctlstatus的参数说明:Loadedservice.service配置文件所在位置Active当前运行状态loaded初始化完成active(running)服务正常运行active(exited)执行命令结束,当前未执行active(waiting)Execution,WaitingforothereventsservicetocompleteinactiveserviceclosedenabledservicestartupautomaticallydisabledservicestartupdoesnotstartstaticservicestartupitemisnotavailablefailedconfigurationerrorMainPID主进程IDMemory内存大小CGroup所有进程详情最后,整理了一些关于systemctl常用命令的资料。startstartstopstopstatusrunningstatusrestartrestartreloadreloadservice,用于更新配置units--type=service启动服务
