How to make sure an application keeps running on Linux

前端 未结 16 1254
清歌不尽
清歌不尽 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:28

    You could make it a service launched from inittab (although some Linuxes have moved on to something newer in /etc/event.d). These built in systems make sure your service keeps running without writing your own scripts or installing something new.

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

    It's even simplier:

    #!/bin/bash
    
    export DISPLAY=:0
    
    process=processname
    makerun="/usr/bin/processname"
    
    if ! pgrep $process > /dev/null
    then
        $makerun &
    fi
    

    You have to remember though to make sure processname is unique.

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

    I think a better solution is if you test the function, too. For example, if you had to test an apache, it is not enough only to test, if "apache" processes on the systems exist.

    If you want to test if apache OK is, then try to download a simple web page, and test if your unique code is in the output.

    If not, kill the apache with -9 and then do a restart. And send a mail to the root (which is a forwarded mail address to the roots of the company/server/project).

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

    Monit is perfect for this :)

    You can write simple config files which tell monit to watch e.g. a TCP port, a PID file etc

    monit will run a command you specify when the process it is monitoring is unavailable/using too much memory/is pegging the CPU for too long/etc. It will also pop out an email alert telling you what happened and whether it could do anything about it.

    We use it to keep a load of our websites running while giving us early warning when something's going wrong.

    -- Your faithful employee, Monit

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

    first of all, how do you start this app? Does it fork itself to the background? Is it started with nohup .. & etc? If it's the latter, check why it died in nohup.out, if it's the first, build logging.

    As for your main question: you could cron it, or run another process on the background (not the best choice) and use pidof in a bashscript, easy enough:

    if [ `pidof -s app` -eq 0 ]; then
        nohup app &
    fi
    
    0 讨论(0)
  • 2020-11-28 19:30

    It's a job for a DMD (daemon monitoring daemon). there are a few around; but I usually just write a script that checks if the daemon is running, and run if not, and put it in cron to run every minute.

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