linux screen編程,自動監控並向screen發送命令

來源:互聯網
上載者:User

#!/bin/bash#author:haiy#email:haiyangfu512@gmail.com# Fri Apr 25 13:04:48 CST 2014
#GNU licence.#monitor and keep the program runningDEBUG=falsefunction restart_program(){#向screen發送要執行的命令    if [  $# -lt 3 ];then        echo $# parameters get!        echo usage: bash restart_program session_name window_index_number command        exit    fi    session=$1    window=$2    command=$3    #echo "current session is $session.$window_num"    screen -x -S $session -p $window -X stuff "$command"}function single_watch(){#檢測某個程式是否在運行    if [  $# -lt 2 ];then        echo $# parameters get!        echo usage: bash single_watch program_name running_instance_number        exit    fi    program_name=$1    expected_number=$2    real_number=$( ps aux | grep "$program_name"$ | grep -v grep | grep -v $0 | grep -v SCREEN | grep -v vim | wc -l )    if [ ! $real_number = $expected_number ];then        echo  false    else        echo true    fi}function keep_running(){#從檔案讀入要檢測啟動並執行程式的列表以及其所在的screen 的session name和window number。    #$1 is the service_list file with service name ,    #screen session name and window index    #$2 is the sleep duration    service_list=$1    sleep_time=$2    while true;     do        if [  $# -lt 2 ];then            echo $# parameters get!            echo usage: bash check_running_service list_file sleep_time            exit        fi        #read the checked service list from the file        while read line;        do            IFS=';' read -a content <<< "$line"            session=$(echo "${content[0]}")            window=$(echo "${content[1]}")            program=$(echo "${content[2]}")            if $DEBUG; then                echo session,$session,                echo window,$window,                echo program,$program            fi            stat=$(single_watch "$program" 1)            if [ $stat == "false" ];then                echo "`date` Gonna restart $program..."                restart_program $session $window "$program"                #echo "$program ." > mail_content                #python gmail_sender.py
                #發送郵件。。。。            else                echo "`date` ==> $line ==>Running normally."            fi        done < $service_list        if $DEBUG;then            exit        fi        echo "I'm gonna sleep a while,$2.."        sleep $sleep_time    done}if $DEBUG;then    #Here the command should be quoted with double quotes    restart_program 16902.watch_dog 3 "ls"    #single_watch    keep_running $1 $2    #session_name=16902.watch_dog     #window_num=3    #screen -x -S $session -p $window_num -X stuff $commandfiif [  $# -lt 2 ];then    echo usage: bash keep_running list_file sleep_time    exitfikeep_running $1 $2=======分隔=====================list_file example: 16902.watch_dog;2;bash test_loop.sh                                                                                                                      


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.