How to make sure an application keeps running on Linux

前端 未结 16 1253
清歌不尽
清歌不尽 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
    
    0 讨论(0)
  • 2020-11-28 19:18

    A nice, simple way to do this is as follows:

    1. Write your server to die if it can't listen on the port it expects
    2. Set a cronjob to try to launch your server every minute

    If it isn't running it'll start, and if it is running it won't. In any case, your server will always be up.

    0 讨论(0)
  • 2020-11-28 19:21

    If you are using a systemd-based distro such as Fedora and recent Ubuntu releases, you can use systemd's "Restart" capability for services. It can be setup as a system service or as a user service if it needs to be managed by, and run as, a particular user, which is more likely the case in OP's particular situation.

    The Restart option takes one of no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, or always.

    To run it as a user, simply place a file like the following into ~/.config/systemd/user/something.service:

    [Unit]
    Description=Something
    
    [Service]
    ExecStart=/path/to/something
    Restart=on-failure
    
    [Install]
    WantedBy=graphical.target
    

    then:

    systemctl --user daemon-reload
    systemctl --user [status|start|stop|restart] something
    

    No root privilege / modification of system files needed, no cron jobs needed, nothing to install, flexible as hell (see all the related service options in the documentation).

    See also https://wiki.archlinux.org/index.php/Systemd/User for more information about using the per-user systemd instance.

    0 讨论(0)
  • 2020-11-28 19:23

    Notice: Upstart is in maintenance mode and was abandoned by Ubuntu which uses systemd. One should check the systemd' manual for details how to write service definition.

    Since you're using Ubuntu, you may be interested in Upstart, which has replaced the traditional sysV init. One key feature is that it can restart a service if it dies unexpectedly. Fedora has moved to upstart, and Debian is in experimental, so it may be worth looking into.

    This may be overkill for this situation though, as a cron script will take 2 minutes to implement.

    #!/bin/bash
    if [[ ! `pidof -s yourapp` ]]; then
        invoke-rc.d yourapp start
    fi
    
    0 讨论(0)
  • 2020-11-28 19:23

    One can install minutely monitoring cronjob like this:

    crontab -l > crontab;echo -e '* * * * * export DISPLAY=":0.0" && for app in "eiskaltdcpp-qt" "transmission-gtk" "nicotine";do ps aux|grep -v grep|grep "$app";done||"$app" &' >> crontab;crontab crontab

    disadvantage is that the app names you enter have to be found in ps aux|grep "appname" output and at same time being able to be launched using that name: "appname" &

    0 讨论(0)
  • 2020-11-28 19:27

    I have used a simple script with cron to make sure that the program is running. If it is not, then it will start it up. This may not be the perfect solution you are looking for, but it is simple and works rather well.

    #!/bin/bash
    #make-run.sh
    #make sure a process is always running.
    
    export DISPLAY=:0 #needed if you are running a simple gui app.
    
    process=YourProcessName
    makerun="/usr/bin/program"
    
    if ps ax | grep -v grep | grep $process > /dev/null
    then
        exit
    else
        $makerun &
    fi
    
    exit
    

    Then add a cron job every minute, or every 5 minutes.

    0 讨论(0)
提交回复
热议问题