zoukankan      html  css  js  c++  java
  • 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 " " runtime: `date '+%Y-%m-%d %H:%M:%S'` " " >> ${log_file} if [[ ! -f ${dir}/${conf_file} ]];then echo -e "33[31m ${dir}/${conf_file} does not exist! 33[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 "33[36m now start jar... 33[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 "33[31m start ${dir}/${msg_jar} occured error, please read ${jar_name} log.33[0m" echo -e "start ${dir}/${msg_jar} occured error, please read ${jar_name} log." >> ${log_file} exit 1 else echo -e "33[36m start ${dir}/${msg_jar} successful, pid is $pid .33[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执行。

  • 相关阅读:
    守护线程
    接口中的方法重写
    jvm内存结构
    浅拷贝,深拷贝
    队列
    12月4号荒度了一天
    同步条件
    条件变量
    信号量Semaphore
    sql练习
  • 原文地址:https://www.cnblogs.com/day1day1up/p/15387280.html
Copyright © 2011-2022 走看看