创建 systemctl 自启动任务

By | 2022年12月21日

1. 创建 Service

/lib/systemd/system 目录下创建 tomcat.service 文件:

[Unit]
Description=Tomcat service.

[Service]
Type=forking
ExecStart=/home/startup.sh
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/home/shutdown.sh

[Install]
WantedBy=multi-user.target graphical.target

tomcat.service 核心就是 startup.sh、shutdown.sh 脚本,添加完后需重新加载配置、设置开机自启动等:

# 设置可执行权限
sudo chmod +x ./startup.sh
sudo chmod +x ./shutdown.sh

# 重新加载配置
sudo systemctl daemon-reload

# 启动服务
systemctl start tpqxb.service

# 服务常用操作(当然配置里需要支持才行)
systemctl start tomcat.service
systemctl stop tomcat.service
systemctl restart tomcat.service
systemctl status tomcat.service

2. 删除 Service

systemctl stop [servicename]
systemctl disable [servicename]
rm /etc/systemd/system/[servicename]
rm /etc/systemd/system/[servicename] # and symlinks that might be related
rm /usr/lib/systemd/system/[servicename] 
rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failed

It is possible that the systemd service ‘wraps’ the old style scripts in /etc/init.d, so you may want to clean that up too, but that is not where systemd services live.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注