Linux 服务管理命令 systemctl

一,简介

CentOS 7 中 systemctl 整合了chkconfig 与 service功能

systemctl is-enabled iptables.service
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务


注:*代表某个服务的名字,如http的服务名为httpd


二,举例

yum -y install httpd


启动服务(等同于service httpd start)

systemctl start httpd.service

停止服务(等同于service httpd stop)

systemctl stop httpd.service

重启服务(等同于service httpd restart)

systemctl restart httpd.service

查看服务是否运行(等同于service httpd status)

systemctl status httpd.service

开机自启动服务(等同于chkconfig httpd on)

systemctl enable httpd.service

开机时禁用服务(等同于chkconfig httpd on)

systemctl disable httpd.service

查看服务是否开机启动 (等同于chkconfig --list)

systemctl list-unit-files (查看开机启动项)
systemctl list-unit-files | grep 程序名称 (查看某些服务开机启动状态)
systemctl list-unit-files | grep enable (查看哪些为开机启动服务)


三,创建自己的服务

创建一个.service 扩展名的文件 node-spider.service

[Unit]
Description=node simple server

[Service]
ExecStart=/usr/bin/node /test/node-spider.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/tmp/node-systemd-demo

[Install]
WantedBy=multi-user.target

Unit区块的Description字段,是服务的简单描述。

Service区块的字段含义如下。

ExecStart:启动命令,ExecStart 后面分别是 node可执行文件的绝对路径、node-spider.js 是我自己写的可执行程序程序。

Restart:如何重启。always表示如果进程退出,总是重启

Environment:环境变量

WorkingDirectory:工作目录

Install区块的WantedBy字段指定,设为开机启动时,该服务所在的 Target 是multi-user.target。


现在将配置文件拷贝到 Systemd 之中。

sudo cp node-spider.service /etc/systemd/system

接着,就启动服务。

sudo systemctl start node-server


修改时间 2021-12-08

声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
WP_Query 函数
如何调整 iconfont 图标的位置和基线
Node.js 18.x 开始支持内置单元测试
curl 的用法指南
WordPress 引入自定义 JavaScript 文件
Linux apt 命令
CSS 改变 svg 图片颜色
MySQL 删除逗号分隔字段中的某一个值