Ubuntu设置开机启动jar包java项目

如果系统安装了jdk环境则不需要手动安装,如果没有安装的话,可以选择apt安装或者是手动安装
apt:
sudo apt install openjdk-8-jre-headless


手动:这是一个32位的包,根据自己的系统环境进行选择。
tar -zxf jdk-8u291-linux-arm32-vfp-hflt.tar.gz -C /usr/local/
vim /etc/profile  
export JAVA_HOME=/usr/local/jdk1.8.0_291
export JRE_HOME=/usr/local/jdk1.8.0_291
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
# 使其生效
source /etc/profile

查看是否安装成功:

java -version

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_服务器


开始部署

1./etc/init.d/下新建脚本pushflow.sh

2.在脚本中一定要添加这些注释,否则报错

** 下面的那个环境变量,如果是yum或者apt安装的则不需要写入,直接可以使用

#!/bin/sh
### BEGIN INIT INFO
# Provides:          pushflow.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/jdk1.8.0_291
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
cd /app/push-flow
echo "jar 包开始部署" 
nohup java -jar push-flow-system-5.1.2.jar > /dev/null 2>&1 &
echo "ok is successful"

3.设置文本权限

sudo chmod 755 /etc/init.d/pushflow.sh

4.将命令脚本添加到启动脚本中去

cd /etc/init.d
sudo update-rc.d pushflow.sh defaults 90
12

注:90代表启动脚本的先后顺序
5.reboot重启生效测试
6.卸载启动脚本

cd /etc/init.d
sudo update-rc.d -f pushflow.sh remove

centos 开机自启动 jar包(自动动shell脚本)

1.编写shell 脚本

vim  /root/.start_jar.sh

#!/bin/bash
### BEGIN INIT INFO
# Provides:          start_jar.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
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
cd /app/back-end
nohup java -jar -Dspring.config.location=application-prod.yml,application-config.yml account-manager-0.0.1-SNAPSHOT-dev.jar > account.log 2>&1 &
cd /app/org-manage
nohup java -jar -Dspring.config.location=application-prod.yml,application-config.yml org-manager-0.0.1-SNAPSHOT-dev.jar  >org.log  2>&1 &

将脚本加入开机自启

vi /etc/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
bash -l /root/.start_jar.sh

最后一行是自己添加的 其余都是系统自带的
bash -l (-l:在启动jar包的时候会先加载环境变量,然后再去启动jar包)
其次就是一定要,将 chmod +x /etc/rc.d/rc.local 添加执行权限

chmod +x /root/.start_jar.sh
chmod +x /etc/rc.d/rc.local

3 最后就是重启系统验证吧。

ubuntu以apt方式安装redis,并设置自启动

状态查看

systemctl status redis

安装redis

sudo apt-get install -y redis-server

设置密码

sudo vim /etc/redis/redis.conf

修改配置文件里
1.注释掉bind 127.0.0.1。将这部注释掉,否则只有本机才能访问
2.保护模式修改为no。protected-mode no 保持默认
3.找到 daemonize no ,修改为 daemonize yes //默认以后台程序方式运行,不需要加&强制后台运行了
修改密码。requirepass xxxxxx
然后再重新启动redis即可

service redis restart

systemctl status redis

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_服务器_02

Ubuntu安装mysql

1.更新源

sudo apt update

2.安装MySql服务器和客户端

sudo apt install mysql-server mysql-client

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_ubuntu_03


3.Ubuntu18.04安装过程中没有设置密码的环节(如果有就下面的不需要做了)

sudo cat /etc/mysql/debian.cnf

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_mysql_04

mysql -u*** -p***

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_linux_05


重置用户和密码

use mysql;  

update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost'; 
 
update user set  plugin="mysql_native_password";     

flush privileges;

quit;

测试

mysql -uroot -p123456

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_服务器_06


5.启动和关闭mysql

启动mysql:

方式一:sudo /etc/init.d/mysql start

方式二:sudo service mysql start

停止mysql:

方式一:sudo /etc/init.d/mysql stop

方式二:sudo service mysql stop

重启mysql:

方式一:sudo/etc/init.d/mysql restart

方式二:sudo service mysql restart

Ubuntu中设置mysql开机自动启动

如果没有开机自动启动,使用下面命令设置开机自动启动

sudo update-rc.d -f mysql defaults

然后输入reboot重启虚拟机

之后再输入sudo service mysql status查看mysql是否自动启动

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_mysql_07


启动成功

mysql基本命令:

查看mysql运行状态:sudo service mysql status
运行mysql:sudo service mysql start
结束mysql:sudo service mysql stop
取消开机启动:sudo update-rc.d -f mysql remove

Ubuntu设置Tomcat开机自启动

1.新建文件tomcat.sh,将上面内容写入文档。JAVA_HOME,WEBSERVER_PATH根据实际情况填写。

(注意第一句#!/bin/sh如果不写,就不是一个shell文件)然后将对应的JAVA_HOME和TOMCAT_HOME换成你自己对应的java目录和tomcat目录就行了。

#!/bin/bash  
# chkconfig: 345 90 10
# description: tomcat8
# processname: tomcat8
export JAVA_HOME=/usr/lib/jdk/jdk1.7.0_72  
TOMCAT_HOME=/usr/local/program_files/tomcat8 
STARTUP_SHELL=$TOMCAT_HOME/bin/startup.sh  
SHUTDOWN_SHELL=$TOMCAT_HOME/bin/shutdown.sh  
case "$1" in  
start)  
sh $STARTUP_SHELL  
;;  
stop)   
sh $SHUTDOWN_SHELL  
;;  
restart)  
sh $SHUTDOWN_SHELL  
sh $STARTUP_SHELL  
;;  
esac

$0 为执行的文件名 $1,$1,$2…依次为第1,第2,第3…个参数。

2.将tomcat.sh文件拷贝到/ect/init.d目录,并命名为tomcat

cp tomcat.sh /etc/init.d/tomcat

3.给其增加可执行权限

chmod +x /etc/init.d/tomcat
sudo sysv-rc-conf tomcat on

没有此命令则安装 apt-get install sysv-rc-conf

检查tomcat启动设置: sysv-rc-conf —list|grep tomcat

ubuntu怎么运行apidemo Ubuntu怎么运行jar包_服务器_08


直接进/etc/rcX.d中,也能发现以S开头的tomcat的启动项。S开头的启动项表示开机启动,K开头的表示开机不启动。

5.如果要取消开机启动,sysv-rc-conf tomcat off
如果要启动项要删除tomcat,sysv-rc-conf tomcat remove

6.重启机器(reboot)。
检查tomcat是否真的在监听端口:
netstat -nltp|grep

7.你终于可以以下面的方式管理tomcat了。
启动:/etc/init.d/tomcat start
关闭:/etc/init.d/tomcat stop
再也不用去一层一层目录里找startup.sh或者catalina.sh了。