一、新建启动JAR包的sh文件
nohup java -jar xxx.jar > xxx.log &
#用你的项目名称代替上述的XXX
#此sh文件,命名为startup.sh
二、设置自启动命令
1.进入 rc.d目录
cd /etc/rc.d
2.执行命令:cat rc.local 查看文件内容
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
###上述代码为rc.local文件中自有内容,不需要改动
3. 执行命令:vim rc.local , 修改rc.local 。按【i】键进入编辑模式,在最后添加代码:
cd /myApp/test
sh /myApp/test/startup.sh
#第一句为进入你项目所在的目录,我这里把项目放在/myApp/test下
#第二句执行该目录下的sh文件
##说明
如果不提前进入所在目录,直接执行第二句,也会开机自启动,但是日志文件会在根目录下的log文件中。只有先进入,再执行,项目的日志文件才会在test文件夹下
##test文件中有jar包、startup.sh、以及jar包的日志文件logs
然后先按下【esc】键,再按下【:】键,在光标闪烁处输入【:wq】。按回车,完成修改
4. 设置执行权限。
chmod +x /etc/rc.d/rc.local
chmod +x /myApp/test/startup.sh
5. 执行命令:reboot,重启服务器
6.重启后执行命令:ps -ef |grep jar ,查看jar包是否自启动成功。