有些时候我们想要在linux启动后自动执行某些命令,如启动redis.mq,微服务等操作,可以参考下列
以我要开机启动jenkins为例,在/usr/lib/systemd/system/ (CentOS 在/usr/lib/systemd/)下创建一个xxx.service
内容如下
jenkins.service
[Unit]
Description=jenkins server daemon
[Service]
#java一定用绝对路径
ExecStart=/usr/bin/java -jar /opt/install/package/jenkins.war --httpPort=8090
#停用杀死当前进程
ExecStop=/bin/kill -HUP $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target```
保存后,可以systemctl进行操作
systemctl start jenkins #启动
systemctl enable jenkins #设置开机启动
systemctl stop jenkins #停止
报错 Executable path is not absolute, ignorin …
命令路径不能为相对路径,要用绝对路径