Linux新建systemd文件用systemctl命令来管理

1. 创建systemd服务文件,vi /etc/systemd/system/serviceName.service
[Unit]
Description=custom service
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/script.sh

[Install]
WantedBy=multi-user.target
2. 新建一个可执行脚本/usr/bin/script.sh,无特殊要求,保证有执行权限即可
#!/bin/bash
echo "Hello World!"
3. 增加执行权限
chmod +x /usr/bin/script.sh
4. 启动服务
systemctl start serviceName
# 或
systemctl start serviceName.service
5. 启用服务并使其开机自启
systemctl enable serviceName
# 或
systemctl enable serviceName.service
6. 备注
# 若systemctl未识别新服务,可尝试重载systemd管理器配置
systemctl daemon-reload

评论

评论正在提交中...请稍后
评论提交成功...