How to make sure an application keeps running on L

2019-01-07 03:08发布

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 dies off for unknown reasons. When we notice we just launch it again, but it's a pain in the rear and some users don't have permission (or the knowhow) to launch it up.

The programmer in me wants to spend a few hours getting to the bottom of the problem but the busy person in me thinks there must be an easy way to detect if an app is not running, and launch it again.

I know I could cron-script ps through grep:

ps -A | grep appname

But again, that's another hour of my life wasted on doing something that must already exist... Is there not a pre-made app that I can pass an executable (optionally with arguments) and that will keep a process running indefinitely?

In case it makes any difference, it's Ubuntu.

标签: linux
15条回答
We Are One
2楼-- · 2019-01-07 03:46

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-07 03:48

Check out 'nanny' referenced in Chapter 9 (p197 or thereabouts) of "Unix Hater's Handbook" (one of several sources for the book in PDF).

查看更多
疯言疯语
4楼-- · 2019-01-07 03:50

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
查看更多
登录 后发表回答