rpm包安装java jar开机自启
1.下载jdk
官网下载地址:https://www.oracle.com/java/technologies/javase-jdk8-downloads.html
2.安装jdk;
rpm -ivh jdk-8u201-linux-x64.rpm
3.检查安装:
java -version
输出Java版本,则不用第4步和第5步
4.配置jdk路径
打开/etc/profile
增加以下内容:
export JAVA_HOME=/usr/java/jdk1.8.0_201
export JAVA_BIN=/usr/java/jdk1.8.0_201/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
5.更新配置
source /etc/profile
6.编写启动 脚本
启动脚本:start.sh
#!/bin/sh
nohup java -jar /home/test/auto-test.jar >test.log 2>&1&
echo $! > /home/test/test.pid
停止脚本:stop.sh
#!/bin/sh
PID=$(cat /home/test/test.pid)
kill -9 $PID
授予脚本权限
chmod +x start.sh
chmod +x stop.sh
7.编写服务脚本
vim /usr/lib/systemd/system/autotest.service
#!/bin/sh
[Unit]
Description=antotest
After=network.target
[Service]
Type=forking
ExecStart=/home/test/start.sh
ExecStop=/home/test/stop.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8.设置服务启动
systemctl enable auto.service #开机自启
systemctl disable auto.service #关闭开机自启
systemctl start auto.service #手动开启
systemctl stop auto.service #手动关闭
systemctl status auto.service #查看状态