systemd使用

一、必备命令

  1. systemctl status nginx.service,查看某个单元的状态。
  2. systemctl start nginx.service,启动某个单元。
  3. systemctl restart nginx.service,重启某个单元。
  4. systemctl stop nginx.service,停止某个单元。
  5. systemctl enable nginx.service,设置开机自启动。
  6. systemctl disable nginx.service,关闭开机自启动。

二、常用命令

  1. systemctl --version,查看版本。
  2. whereis systemctl,查看位置。
  3. systemctl list-unit-files,列出所有可用单元(服务)。
  4. systemctl list-units,列出所有运行中的单元。
  5. systemctl --failed,列出所有失败的单元。
  6. systemctl list-unit-files | grep enable,查看自启动的软件。
  7. systemctl is-enabled nginx.service,查看某个单元是否开机启动。
  8. systemctl daemon-reload,修改了某个单元的配置文件后,重载配置文件。
  9. systemctl reload nginx.service,重载某个单元。
  10. systemctl kill nginx,杀死单元。

三、*.service 文件配置:

 centos7 创建文件目录:/etc/systemd/system/

例如:创建nginx.service

vi /etc/systemd/system/nginx.service

编辑内容

[Unit]

Description=nginx

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

Privatetmp=true

[Install]

WantedBy=multi-user.target

 

  • [Unit]:服务的说明
  • Description:描述服务
  • After:描述服务类别
  • [Service]服务运行参数的设置
  • Type=forking是后台运行的形式
  • ExecStart为服务的具体运行命令
  • ExecReload为重启命令
  • ExecStop为停止命令
  • PrivateTmp=True表示给服务分配独立的临时空间
  • 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
  • [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

猜你喜欢

转载自www.cnblogs.com/corexy/p/12750490.html