systemctl

 

centos6

centos7

启动

service NAME start

systemctl start NAME[.service]

停止

service NAME stop

systemctl stop NAME[.service]

重启

service NAME restart

systemctl restart NAME[.service]

重载

server NAME reload

systemctl reload NAME[.service]

状态

service NAME status

systemctl status NAME[.service]

条件式重启(启动重启,不启动不重启)

service NAME condrestart

systemctl try-restart NAME[.service]

查看某服务当前状态

systemctl is-active NAME[.service]

查看所有已经激活的服务[所有]

systemctl list-units –type service [--all]

设定开机启动

chkconfig NAME on

systemctl enable NAME[.service]

禁止开机启动

chkconfig NAME off

systemctl disable NAME[.service]

查看所有开机启动

chkconfig --list

systemctl list-unit-files –type service

查看某服务是否开机紫气

systemctl is-enabled NAME[.service]

查看运行级别

runlevel

systemctl list-units –type target

查看默认运行级别

/etc/inittab

systemctl get-default

修改默认级别

systemctl set-default NAME.target

切换至紧急救援模式

systemctl rescue

使用systemctl管理服务

在/usr/lib/systemd/system/目录下创建xxx.service名字的文件。创建完成后systemctl daemon-daload

使用systemctl enable 服务的时候会在/etc/systemd/system目录下创建快捷方式,指向/usr/lib/systemd/system/的脚本

文件中的内容(命令必须为绝对路径)

[Unit]   #启动顺序与依赖关系
Description=当前服务的描述信息
Documentation=当前服务的文档信息
After=network.target #设定当前服务应该在那些服务启动后才启动,只涉及启动顺序,没有依赖
#Before=   设置当前服务应该在哪个服务之前启动
#Wants=  希望和哪个服务同时运行,弱依赖
#Requires= 必须和哪个服务同时运行,强依赖,如果那个服务停止,当前服务退出

[Service]   #启动行为,*****命令都为绝对路径******
EnvironmentFile=-/etc/sysconfig/sshd  #指定当前服务的环境参数文件。
#该文件内部的key=value键值对,可以用$key的形式,在当前配置文件中获取。
#-代表若果该配置文件没有,也不会报错

PIDFile=/usr/local/xxx.pid  #指定PID文件位置,后面可以根据这个PID做重启和停止动作

ExecStart=/usr/sbin/sshd -D $OPTIONS  定义启动进程时执行的命令。$OPTIONS就来自EnvironmentFile字段指定的环境参数文件。

ExecReload=/bin/kill -HUP $MAINPID    #重启服务
ExecStop=/bin/kill -s QUIT $MAINPID   #停止服务
ExecStartPost=/usr/bin/sleep 1    
#有时候systemd会在服务启动之前,还没来得及创建PID文件就结束,
#这会导致systemd找不到该服务的PID文件,无法做管控,这里睡上一秒保证systemd能找到这个PID文件

#ExecReload字段:重启服务时执行的命令
#ExecStop字段:停止服务时执行的命令
#ExecStartPre字段:启动服务之前执行的命令
#ExecStartPost字段:启动服务之后执行的命令
#ExecStopPost字段:停止服务之后执行的命令

Type=simple   #启动类型
#simple(默认值):ExecStart字段启动的进程为主进程
#forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程
#下面不常用
#oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
#dbus:类似于simple,但会等待 D-Bus 信号后启动
#notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
#idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合
#工作目录
WorkingDirectory=/data/portal/
KillMode=process    #重启行为
#control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
#process:只杀主进程,很少用,ssh是这个
#mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
#none:没有进程会被杀掉,只是执行服务的 stop 命令。

Restart=on-failure  #失败时的重启策略
#no(默认值):退出后不会重启
#on-success:只有正常退出时(退出状态码为0),才会重启
#on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
#on-abnormal:只有被信号终止和超时,才会重启
#on-abort:只有在收到没有捕捉到的信号终止时,才会重启
#on-watchdog:超时退出,才会重启
#always:不管是什么退出原因,总是重启

RestartSec=1s  #systemd重启服务前需要等待的时间

#打开文件限制
LimitNOFILE=655360
LimitNPROC=655360

#解除内存限制
LimitMEMLOCK=infinity


[Install]  #开机自动启动
WantedBy=multi-user.target    #多用户模式
#使用systemctl enable xxxx的时候会自动在这个target模式下创建个链接指向该配置文件
#Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
#WantedBy=graphical.target    #图形用户模式 

  

prometheus启动脚本

[Unit]
Description=prometheus
Documentation=prometheus.io
After=network.target

[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
Type=simple
Restart=on-failure

[Install]
WantedBy=multi-user.target                

  

elasticsearch启动脚本

[Unit]
Description=elasticsearch

[Service]
User=es
LimitNOFILE=655360
LimitNPROC=655360
LimitMEMLOCK=infinity
ExecStart=/data/elasticsearch/bin/elasticsearch

[Install]
WantedBy=multi-user.target

  

 jar包做成systemd方式

1.先配置jar的启动脚本,目录在/data/gateway下

[centos@gateway]$ cat start-gateway.sh 
/usr/local/java/bin/java -Xms500m -Xmx500m -jar /data/gateway/gateway.jar --spring.application.name=gateway --spring.profiles.active=product > /data/gateway/gateway.log

2.再配置jar的service

[centos@gateway]$ cat /etc/systemd/system/gateway.service 
[Unit]
Description=gateway
Documentation=http://www.xxx.com
After=network.target

[Service]
User=developer
Group=developer
WorkingDirectory=/data/gateway/
ExecStart=/bin/bash /data/gateway/start-gateway.sh
Type=simple
Restart=on-failure

[Install]
WantedBy=multi-user.target

强依赖型:生产实例

  emqx需要使用mysql插件进行认证,所以在开机的时候emqx就需要自行启动连接mysql,但是当emqx无法连接mysql时并不会导致emqx停止,仅仅是停掉了这个插件,这会影响到后续所有业务,所以需要在emqx的服务文件中加入强依赖和启动顺序

[Unit]
Description=emqx
After=mysqld.service
Requires=mysqld.service

  加上after和requires之后的情况

  • mysql没有启动: emqx会主动拉起mysql服务,在mysql服务启动完成之后在启动emqx
  • mysql已经启动: emqx正常启动
  • mysql启动失败: emqx因为依赖关系,启动失败

额外事项:

  需要nacos提前启动的服务,比如jar包做成的服务,可以不添加强依赖,因为jar无法连接到配置文件会直接重启,不像emqx连不到mysql仅仅停掉一个插件.

 

  

初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
原文地址:https://www.cnblogs.com/forlive/p/9223648.html