【Linux】使用 systemctl 控制服务开机自启动
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
- 准备: hello.service && root 权限
【hello.service】
[Unit]
Description=Lanuch hello Service
After=hello.service
[Service]
Type=idle
Restart=on-failure
WorkingDirectory=/www/hello/bin #bin文件路径
Environment="LD_LIBRARY_PATH=/usr/lib64:/www/hello/lib" #还未加载/etc/bash_profile | bin文件需要的环境变量,动态库,系统库
ExecStart=/www/hello/bin/hello #bin文件
StandardOutput=null #重定向日志打印,不设置该项服务日志将会打印到/var/log/message ,该目录一般较小,很容易打满搞挂机器,一定记得设置
[Install]
WantedBy=multi-user.target
-
加入 /etc/systemd/system/hello.service
-
刷新systemctl 控制服务
systemctl daemon-reload 将hello.service 加入systemctl管理 -
查看服务状态是否添加成功
systemctl status hello.service
-
将服务加入开机自启动
systemctl enable hello.service
-
启动 & 停止 服务
systemctl start hello.service
systemctl stop hello.service