Linux(Ubuntu)下设置开机自启shell脚本执行Java程序jar包
Linux(Ubuntu)下设置开机自启shell脚本执行Java程序jar包
原创
经常需要在Linux服务器上设置开机启动jar包,首先编写一个启动jar包的脚本,这里以/mydata/app/admin为例,文件名叫startup.sh
1.进入到/mydata/app/admin下
cd /mydata/app/admin
2.查看JAVA_HOME目录,因为要用到jdk,没安装请自行安装
echo $JAVA_HOME
- 1.
3.创建自启脚本startup.sh
vi startup.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides: autoJar.sh
# Required-start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the svnd.sh daemon
# Description: starts svnd.sh using start-stop-daemon
### END INIT INFO
export JAVA_HOME=/usr/local/java/jdk1.8.0
export PATH=${JAVA_HOME}/bin:$PATH
cd /mydata/app/admin
nohup java -jar mall-admin-1.0-SNAPSHOT.jar &
### BEGIN INIT INFO
# Provides: autoJar.sh
# Required-start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the svnd.sh daemon
# Description: starts svnd.sh using start-stop-daemon
### END INIT INFO
export JAVA_HOME=/usr/local/java/jdk1.8.0
export PATH=${JAVA_HOME}/bin:$PATH
cd /mydata/app/admin
nohup java -jar mall-admin-1.0-SNAPSHOT.jar &
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
注意,这里的export设置环境变量不可少
4.设置权限
chmod 755 startup.sh
5.将脚本文件放到启动目录(/etc/init.d)下
mv startup.sh /etc/init.d
6.更新优先级
cd /etc/init.d
sudo update-rc.d startup.sh defaults 90
7.卸载启动脚本
cd /etc/init.d
sudo update-rc.d -f startup.sh remove