由于外界存在各种不确定的因素,就会导致服务器关机,项目需要重新启动。此类似问题遇到过几次,就此决定记录。
1.首先在/etc/rc.d/init.d目录编写自启动脚本autojar.sh
#!/bin/sh
# chkconfig: 2345 85 15
# description:auto_run
#程序名
RUN_NAME=“xxxx.jar"
#jar 位置
JAVA_OPTS=/home/xxx/xxxx.jar
LOG_OPTS=/home/xxx/zen.log
#开始方法
start() {
source /etc/profile; nohup java -jar $JAVA_OPTS >$LOG_OPTS 2>&1 &
echo "$RUN_NAME started success."
}
#结束方法
stop() {
echo "stopping $RUN_NAME ..."
kill -9 `ps -ef|grep $JAVA_OPTS|grep -v grep|grep -v stop|awk '{print $2}'`
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Userage: $0 {start|stop|restart}"
exit 1
esac
2.给脚本autojar.sh和jar包xxxx.jar 添加权限
sudo chmod +x /etc/init.d/autojar.sh
sudo chmod +x /home/xxx/xxxx.jar
3.添加到chkconfig 并把chkconfig作为系统服务 然后设置开机自启
3.1 添加为系统服务
sudo chkconfig --add autojar.sh
3.2 设置为开机启动
sudo chkconfig autojar.sh on
3.3 启动服务 查看脚本是否正确
sudo service autojar.sh start
注意:
- Linux的运行级别和chkconfig用法(https://www.cnblogs.com/terryguan/p/4551012.html)
- linux chkconfig命令 开机启动 运行级别 service sysv-rc-conf(https://blog.csdn.net/qq_16234613/article/details/53924545)
- 如果linux 使用的是子账户,考虑到权限不够,上传文件就会被提示,所有我们需要把文件夹设置为全权限(linux 里显示是绿色)
步骤:(1)创建一个文件夹 mkdir test,并在test文件加下创建子文件夹test1
(2) 修改test文件夹的权限为777,chmod 777test,只修改了test目录本身的权限为600,test的子
目录test1权限还是默认权限
(3)修改test目录及其子目录的权限都为600,执行命令chmod -R 777 /home/mypackage
sudo chmod -R 777 文件名