linux系统开机systemctl自动启动项目服务
前言
作为一名开发人员,都会懂一些服务器相关知识。对于项目部署时,自动启动应该是最基本的,之前都是使用inid来进行开机服务。而今systemctl服务逐渐取代前者。本篇简单记录一下systemctl服务的一些配置以及命令。
一、关于systemctl命令
下面以nginx服务为例
systemctl enable nginx.service #开启开机自启动 systemctl disable nginx.service #停止开机自启动 systemctl start nginx.service #启动 nginx 服务 systemctl stop nginx.service #停止 nginx 服务 systemctl restart nginx.service #重启 nginx 服务 systemctl status nginx.service #查看服务当前状态 systemctl list-units --type=service #查看所有已启动的服务 systemctl list-unit-files |grep nginx #查看是否正确启动
sudo systemctl daemon-reload #刷新配置system目录
二、相关配置
下面 nginx.service 文件配置,复制到 /etc/systemd/system 或者 /usr/lib/systemd/system 目录下
[Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target
[Unit]
Description:对该服务的描述;
Documention:说明文档;
Before:在a.service服务启动前,启动本服务;
After:在b.target服务组启动后,再启动本服务;
Wants:弱依赖于c.service,即使被依赖服务启动失败或停止,本服务仍然运行;
Requires:强依赖于d.service,如果被依赖服务启动失败或停止,本服务也会停止。
[Service]
EnvironmentFile:服务的参数文件,形成$OPTIONS;
ExecStart: 服务启动命令
ExecReload: 服务重启命令
ExecStop: 服务停止命令
Type:服务启动类型。默认simple表示ExecStart为主进程,notify类似于simple,启动结束后会发出通知信号。另外还有forking,oneshot,dbus,idle等类型;
KillMode:服务停止类型,默认control-group停止时杀死所有子进程,process只杀主进程,none只停止服务,不杀进程;
Restart:服务重启类型,默认no不重启,on-success正常退出时重启,on-failure非正常退出时重启,还有always,on-abnormal,on-abort等;
RestartSec:间隔多久重启服务。
[Install]
WantedBy:服务所在的服务组。
三、其他项目的配置
java项目配置(若需要配置jvm可以参考最后的链接,里面有涉及environment的配置)
[Unit] Description=java 8083 project After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/bin/bash -c "nohup java -jar /tool/jar/qtz.jar > /tool/jar/xxx.log 2>&1 &" ExecStop=/bin/kill -s QUIT $MAINPID [Install] WantedBy=multi-user.target
node项目配置
[Unit] Description=node 8003 project After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/bin/bash -c "nohup node /tool/node/main.js > /tool/node/xxx.log 2>&1 &" ExecStop=/bin/kill -s QUIT $MAINPID [Install] WantedBy=multi-user.target
四、一些问题
报错service start operation timed out. Terminating ,这个错误大致意思 ‘命令执行超时终止’。以下是解决方式
解决这个问题,借助“bash -c”,bash -c的作用时将一个长字符串当作一条完整的命令来执行,如果在脚本路径后面加上后台运行符号(&),
脚本将就会在后台运行,不会一直处于挂起状态,systemd也就不会一直等待脚本执行完成了。经过测试,可以解决该问题。
将
ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet
修改为
ExecStart=/bin/bash -c "/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet &"
参考资料
https://blog.51cto.com/u_16213367/7366081
https://blog.csdn.net/baidu_37359582/article/details/124367500
https://blog.csdn.net/qq_41035283/article/details/122793745
https://blog.csdn.net/sinat_28371057/article/details/113012352
https://blog.csdn.net/weixin_46564680/article/details/115369090
https://blog.csdn.net/weixin_39992480/article/details/102761989
https://blog.csdn.net/cike626/article/details/122630521
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库