How to make sure an application keeps running on Linux

前端 未结 16 1361
清歌不尽
清歌不尽 2020-11-28 18:51

I\'m trying to ensure a script remains running on a development server. It collates stats and provides a web service so it\'s supposed to persist, yet a few times a day, it

16条回答
  •  醉话见心
    2020-11-28 19:18

    I couldn't get Chris Wendt solution to work for some reason, and it was hard to debug. This one is pretty much the same but easier to debug, excludes bash from the pattern matching. To debug just run: bash ./root/makerun-mysql.sh. In the following example with mysql-server just replace the value of the variables for process and makerun for your process.

    • Create a BASH-script like this (nano /root/makerun-mysql.sh):
    #!/bin/bash
    process="mysql"
    makerun="/etc/init.d/mysql restart"
    if ps ax | grep -v grep | grep -v bash | grep --quiet $process
    then
        printf "Process '%s' is running.\n" "$process"
        exit
    else
        printf "Starting process '%s' with command '%s'.\n" "$process" "$makerun"
        $makerun
    fi
    exit
    
    • Make sure it's executable by adding proper file permissions (i.e. chmod 700 /root/makerun-mysql.sh)

    • Then add this to your crontab (crontab -e):

    # Keep processes running every 5 minutes
    */5 * * * * bash /root/makerun-mysql.sh
    

提交回复
热议问题