systemd常用配置
0
配置linux
服务
vi /etc/systemd/system/service.service
内容:
[Unit]
Description=描述信息
After=network.target network-online.target
Wants=network.target network-online.target
[Service]
User=root
# forking:本身就是后台进程
Type=simple|forking|notify
KillMode=process
ExecStart=启动命令
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
# always:如果不是关闭自动重启
Restart=no|always|on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
然后加载服务:
systemctl daemon-reload
配置里面所有命令需要使用完整路径,否者可能提示下面错误:
May 09 15:44:02 acgist systemd[1]: service.service: main process exited, code=exited, status=127/n/a
May 09 15:44:02 acgist systemd[1]: Unit service.service entered failed state.
May 09 15:44:02 acgist systemd[1]: service.service failed.
[root@acgist /]# systemctl status service
● service.service - www.acgist.com
Loaded: loaded (/etc/systemd/system/service.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Mon 2022-05-09 15:44:07 CST; 1s ago
Process: 27558 ExecStart=/data/www/startup.sh (code=exited, status=127)
Main PID: 27558 (code=exited, status=127)
May 09 15:44:07 acgist systemd[1]: service.service: main process exited, code=exited, status=127/n/a
May 09 15:44:07 acgist systemd[1]: Unit service.service entered failed state.
May 09 15:44:07 acgist systemd[1]: service.service failed.
Java脚本设置
Java应用(包含SpringBoot)需要借助nohup
才能实现关闭和重启,否者不能正常结束任务。启动命令如下:
/usr/bin/nohup /data/software/jdk-17.0.2/bin/java $JAVA_OPTS -jar ./lib/acgist-1.0.0.jar > /dev/null 2>&1 &
内存显示
# 全局配置
vim /etc/systemd/system.conf
DefaultCPUAccounting=yes
DefaultMemoryAccounting=yes
systemctl daemon-reexec
# 独立服务
服务配置service.service[service]节点下面添加
CPUAccounting=yes
MemoryAccounting=yes