linux开机 自动挂载和启动jar包

一、 自动挂载和启动jar包

1、开机配置文件增加命令

vi /etc/rc.local

增加 sh  /home/startup.sh(需要启动的脚本)

2、startup.sh

#!/bin/sh
# 获取当前脚本所在路径
dir=$(cd `dirname $0`; pwd)
jar_name="test.jar"
conf_file="mount.conf"
file_path=${dir}/${conf_file}
log_file=${dir}/"startup.log";
# 加载环境配置 source
/etc/profile source ~/.bash_profile java_path=${JAVA_HOME} sudo echo -e "\n" runtime: `date '+%Y-%m-%d %H:%M:%S'` "\n" >> ${log_file} if [[ ! -f ${dir}/${conf_file} ]];then echo -e "\033[31m ${dir}/${conf_file} does not exist! \033[0m" echo -e "${dir}/${conf_file} does not exist! " >> ${log_file} exit 1 fi # 挂载
# sed 过滤'#'开头的;while read -a line 逐行读取, $line第一列, ${line[1]}第二列
sed -e '/^#/d' ${file_path} | more | while read -a line || [ -n "$line" ]; do
# -n 不为空,-a 且 if [ -n "$line" -a -n "${line[1]}" ]; then echo mount "$line ${line[1]}" >> ${log_file}; mount "$line" "${line[1]}"; fi done; # 启动jar cd ${dir}echo -e "\033[36m now start jar... \033[0m" pid=$(pgrep -f ${msg_jar}) if [[ -n $pid ]];then kill -9 $pid fi
# nohup 程序不挂起
nohup ${java_path}/bin/java -jar ${dir}/${msg_jar} & sleep 10 pid=$(pgrep -f ${msg_jar}) if [[ -z $pid ]];then echo -e "\033[31m start ${dir}/${msg_jar} occured error, please read ${jar_name} log.\033[0m" echo -e "start ${dir}/${msg_jar} occured error, please read ${jar_name} log." >> ${log_file} exit 1 else echo -e "\033[36m start ${dir}/${msg_jar} successful, pid is $pid .\033[0m" echo -e "start ${dir}/${msg_jar} successful, pid is $pid ." >> ${log_file} fi

二、sudo 执行命令,获取不到环境配置

1、sudo visudo

修改 Defaults env_reset 为 Defaults !env_reset,不让sudo重置环境变量

2、sudo vi ~/.bashrc

增加 alias sudo=sudo PATH=$PATH

执行 source ~/.bashrc 生效

3、sudo java -version或者 java -jar 就能通过sudo执行。

 

posted @ 2021-10-09 19:12  暗夜消辰  阅读(388)  评论(0编辑  收藏  举报