zoukankan      html  css  js  c++  java
  • Linux后台进程启停脚本模板

    图片来源于网络

    目录

    在Linux上启动程序后台运行时,往往需要输入一堆复杂的命令,为了能快速编写一个完善的启动脚本,整理一个通用的启停脚本模板如下。
    脚本支持从任意位置执行,不存在路径问题,启动脚本和停止脚本最好一起配合使用。

    启动脚本

    #!/bin/bash
    
    current_path=$(cd `dirname $0`; pwd)
    parent_path=$(cd ${current_path} ; cd ..; pwd)
    conf_path=$parent_path/conf
    log_path=$parent_path/log
    #echo $current_path
    #echo $parent_path
    
    pid_file=$current_path/xxx.pid
    arg1=$1
    arg2=$2
    
    function print_usage() {
        echo ""
        echo "Usage:"
        echo "    sh $0 <arg1> <arg2>"
        echo ""
        echo "  e.g:"
        echo "    sh $0 yyy zzz"
        echo ""
    }
    
    if [[ ! "${arg1}" ]]; then
        print_usage
        exit 1
    fi
    
    if [[ ! "${arg2}" ]]; then
        print_usage
        exit 1
    fi
    
    #print_usage
    
    if [[ -f "${pid_file}" ]]; then
        pid=`cat ${pid_file}`
        pid_exist=$(ps aux | awk '{print $2}'| grep -w $pid)
        if [[ "$pid_exist" ]]; then
            echo ""
            echo "process is running: ${pid}, Please stop it first!"
            echo ""
            exit 1
        fi
    fi
    
    echo "startup..."
    echo "arg1: ${arg1}, arg2: ${arg2}"
    
    ## 启动Python进程示例
    #nohup python ${parent_path}/test_tasks.py -s ${arg1} -c ${arg2}  > /dev/null 2>&1 & echo $! > $pid_file
    
    ## 启动Java进程示例
    ## set java path
    #if [ -z "$JAVA" ]; then
    #	JAVA=$(which java)
    #fi
    
    #if [ -z "$JAVA" ]; then
    #	echo "java not install, Please install java first" 2>&2
    #	exit 1
    #fi
    
    ## set classpath
    #CLASSPATH=$parent_path/conf
    #for i in $parent_path/libs/*;
    #do
    #	CLASSPATH="$CLASSPATH":$i
    #done
    
    #OPTS_APP="-Dlogback.configurationFile=$conf_path/logback.xml -Dlog.dir=$log_path"
    #OPTS_MEM="-Xms1024m -Xmx1024m"
    #OPTS_JAVA_EXT="-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"
    #OPTS_JAVA="$OPTS_JAVA_EXT $OPTS_MEM $OPTS_APP"
    
    ## 方式1: 明确指定main方法所在的入口类
    #nohup $JAVA $OPTS_JAVA -classpath $CLASSPATH xxx.yyy.zzz.BootstrapClass > /dev/null 2>&1 & echo $! > $pid_file
    
    ## 方式2: 直接指定可执行jar包
    #nohup $JAVA $OPTS_JAVA -jar xxx.jar > /dev/null 2>&1 & echo $! > $pid_file
    
    sleep 1
    echo "started."
    echo "process id: `cat ${pid_file}`"
    echo ""
    

    根据实际情况,将脚本模板中的xxxarg1arg2修改为对应的名称即可。

    停止脚本

    #!/bin/bash
    
    current_path=$(cd `dirname $0`; pwd)
    #echo $current_path
    
    pid_file=$current_path/xxx.pid
    
    if [[ ! -f "${pid_file}" ]]; then
        echo "pid file not exists"
        echo ""
        exit 1
    fi
    
    echo "stopping..."
    pid=`cat ${pid_file}`
    echo "process id: ${pid}"
    kill -15 $pid
    sleep 1
    
    # check process exists
    pid_exist=$(ps aux | awk '{print $2}'| grep -w $pid)
    if [[ ! "${pid_exist}" ]]; then
        # echo the process $pid is not exist
        rm -rf ${pid_file}
    else
        # echo the process $pid exist
        kill -9 $pid
        sleep 1
        rm -rf ${pid_file}
    fi
    
    echo "stopped."
    

    根据实际情况,将脚本模板中xxx修改为对应名称即可。

    【参考】
    https://blog.csdn.net/baidu_38558076/article/details/88191567 shell 判断pid是否真实存在
    https://blog.csdn.net/sunt2018/article/details/102523112 后台启动 nohup,并记录pid号


    作者:编程随笔
    出处:http://www.cnblogs.com/nuccch/
    声明:本文版权归作者和博客园共有,欢迎转载,但请在文章页面明显位置给出原文连接。

  • 相关阅读:
    Count and Say leetcode
    Find Minimum in Rotated Sorted Array II leetcode
    Find Minimum in Rotated Sorted Array leetcode
    Search in Rotated Sorted Array II leetcode
    search in rotated sorted array leetcode
    Substring with Concatenation of All Words
    Subsets 子集系列问题 leetcode
    Sudoku Solver Backtracking
    Valid Sudoku leetcode
    《如何求解问题》-现代启发式方法
  • 原文地址:https://www.cnblogs.com/nuccch/p/15076912.html
Copyright © 2011-2022 走看看