一、必备命令
- systemctl status nginx.service,查看某个单元的状态。
- systemctl start nginx.service,启动某个单元。
- systemctl restart nginx.service,重启某个单元。
- systemctl stop nginx.service,停止某个单元。
- systemctl enable nginx.service,设置开机自启动。
- systemctl disable nginx.service,关闭开机自启动。
二、常用命令
- systemctl --version,查看版本。
- whereis systemctl,查看位置。
- systemctl list-unit-files,列出所有可用单元(服务)。
- systemctl list-units,列出所有运行中的单元。
- systemctl --failed,列出所有失败的单元。
- systemctl list-unit-files | grep enable,查看自启动的软件。
- systemctl is-enabled nginx.service,查看某个单元是否开机启动。
- systemctl daemon-reload,修改了某个单元的配置文件后,重载配置文件。
- systemctl reload nginx.service,重载某个单元。
- 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