bash shell处理文件的顺序如下:
/etc/rc.local
/etc/profile 用户环境变量
/etc/profile.d等待配置文件
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
/etc/rc.local配置:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#exit 0
#source /etc/profile 无法加载用户环境变量
docker start mysql2
docker start emq1
docker start rabbitmq1
docker start redis1
sleep 20
sh /data/springboot/rundev > /root/1.log
sh /data/springboot/runtimer > /root/2.log
exit 0
当开机自启动无法加载用户环境变量时,可以直接在脚本文件中指定Java绝对路径,如下:
#!/bin/sh
echo "Restarting SpringBoot Application"
varName=device-admin
varJar=${varName}*.jar
pid=`ps -ef | grep java | grep ${varName} | awk '{print $2}'`
if [ -n "$pid" ]
then
kill -9 $pid
echo "关闭进程:"$pid
fi
echo $JAVA_HOME
echo "授予当前用户权限"
chmod 777 /data/springboot/${varJar}
echo "执行....."
/data/jdk1.8.0_221/bin/java -jar /data/springboot/${varJar}>/dev/null 2>&1 &
echo "success"