Centos7编写脚本开机自动启动多个jar包
一 需要启动的jar包
yt_transport_service_source-V1.0.21.jar transport-task-1.0-SNAPSHOT.jar transport-web-java-1.0-SNAPSHOT.jar yt_transport_web_source-1.0.1.jar
二 编写脚本文件
该脚本文件的存放位置和名字都是任意的,此处起名为allstart.sh,存放位置为当前目录。
vim allstart.sh
在打开的文件中输入以下代码信息,保存并退出:
#!/bin/bash export JAVA_HOME=/opt/jdk export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH nohup java -jar /usr/local/web/platform/transport_service_source_7098/yt_transport_service_source-V1.0.21.jar --server.port=7098 > /usr/local/web/platform/transport_service_source_7098/yt_transport_service_source.log 2>&1 & nohup java -jar /usr/local/web/platform/transport_task_18070/transport-task-1.0-SNAPSHOT.jar --server.port=18070 > /usr/local/web/platform/transport_task_18070/transport-task.log 2>&1 & nohup java -jar /usr/local/web/platform/transport_web_8060/transport-web-java-1.0-SNAPSHOT.jar --server.port=8060 > /usr/local/web/platform/transport_web_8060/transport-web.log 2>&1 & nohup java -jar /usr/local/web/platform/transport_web_source_8088/yt_transport_web_source-1.0.1.jar --server.port=8088 > /usr/local/web/platform/transport_web_source_8088/yt_transport_web_source.log 2>&1 &
解释:
/usr/local/web/platform/transport_service_source_7098/yt_transport_service_source-V1.0.21.jar是jar包的绝对路径; --server.port=7098是服务的端口号; /usr/local/web/platform/transport_service_source_7098/yt_transport_service_source.log是日志的输出路径。
操作说明:
1.如果没有vim工具,可以下载一个,使用yum install vim命令即可下载;或者将vim命令改成vi也是可以的,vi是系统自带的编辑工具。
2.编辑文件,首先得按一下i键,进入编辑,编辑完之后按esc键退出编辑,最后按:wq保存并退出。
三 给allstart.sh添加执行权限
chmod +x allstart.sh
四 编辑rc.local文件
vim /etc/rc.d/rc.local
在rc.local文件末尾添加allstart.sh文件的路径,保存并退出:
#!/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 /usr/local/web/platform/allstart.sh
五 授予rc.local文件执行权限
chmod +x /etc/rc.d/rc.local
六 重启
reboot
七 重启之后查看jar包是否启动
[root@localhost platform]# ps -ef | grep java root 1572 1 0 10:59 ? 00:02:52 java -jar /usr/local/web/platform/transport_service_source_7098/yt_transport_service_source-V1.0.21.jar --server.port=7098 root 1574 1 0 10:59 ? 00:01:22 java -jar /usr/local/web/platform/transport_task_18070/transport-task-1.0-SNAPSHOT.jar --server.port=18070 root 1580 1 0 10:59 ? 00:03:08 java -jar /usr/local/web/platform/transport_web_source_8088/yt_transport_web_source-1.0.1.jar --server.port=8088 root 1609 1 0 10:59 ? 00:02:17 java -jar /usr/local/web/platform/transport_eureka_8000/spring-cloud-eureka.jar --server.port=8000 root 5404 4945 0 11:40 pts/0 00:02:01 java -jar /usr/local/web/platform/transport_web_8060/transport-web-java-1.0-SNAPSHOT.jar --server.port=8060 root 23572 21935 0 16:13 pts/1 00:00:00 grep --color=auto java
至此,jar包开机自动启动脚本编写完毕~
都看到这了,动动你的小手点个赞吧!