centos7 jar包开机自启
时间: 2023-09-02 11:11:57 浏览: 111
要使一个 jar 包在 CentOS 7 开机自启动,可以按照以下步骤进行操作:
1. 首先,将你的 jar 包放在一个合适的位置,例如 `/opt/myapp/myapp.jar`。
2. 创建一个启动脚本,比如 `/etc/init.d/myapp`,并将以下内容复制进去:
```
#!/bin/bash
#
# myapp This shell script takes care of starting and stopping
# the myapp daemon
#
# chkconfig: - 85 15
# description: myapp daemon
# processname: myapp
# pidfile: /var/run/myapp.pid
# Source function library.
. /etc/rc.d/init.d/functions
prog="myapp"
start() {
echo -n $"Starting $prog: "
daemon java -jar /opt/myapp/myapp.jar > /dev/null &
echo $! > /var/run/myapp.pid
success && echo
failure && echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p /var/run/myapp.pid
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
```
这个脚本会启动一个后台的 Java 进程,并将其 PID 写入 `/var/run/myapp.pid`。
3. 接下来,将脚本添加到系统服务中:
```
sudo chmod +x /etc/init.d/myapp
sudo chkconfig --add myapp
```
现在,你的 jar 包应该会在 CentOS 7 开机时自动启动了。你可以使用以下命令来测试它:
```
sudo service myapp start
sudo service myapp stop
sudo service myapp restart
```
阅读全文