在/etc/systemd/system/
目录下创建一个新的<your_service_name>.service
文件,例如buda.service
,里面填入以下内容:
(特别要注意设置WorkingDirectory, 这将决定exe程序中的相对路径的解析结果。)
[Unit]
Description=Buda Website Service
After=network.target
[Service]
Type=simple
WorkingDirectory=/path/to/working/directory/
ExecStart=/path/of/exe_file
StandardOutput=append:/var/log/buda/2024_11_01.log
StandardError=append:/var/log/buda/2024_11_01.log
Restart=always
RestartSec=5
User=root
Group=root
[Install]
WantedBy=multi-user.target
然后执行下面的命令让服务生效,查看错误日志
# 重新加载systemd管理器配置:
systemctl daemon-reload
systemctl enable buda
systemctl start buda
systemctl status buda
systemctl stop buda
systemctl disable buda
#查看服务的日志输出,帮助调试和故障排除。
journalctl -u buda
作者寄语
以上如有错漏之处,敬请大家指正。我是主修C/C++、Vue3开发的程序员,我的联系方式:
微信:TobeBuda
Email/Paypal: jinmin.si@outlook.com
参考资料
stackoverflow
chatgpt-4o
gemini