如何确保应用程序在 Linux 上持续运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/298760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:44:05  来源:igfitidea点击:

How to make sure an application keeps running on Linux

linux

提问by Oli

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 couldcron-script ps through grep:

我知道我可以通过 grep 编写 ps 脚本:

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.

如果它有任何区别,那就是 Ubuntu。

采纳答案by JimB

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

注意:Upstart 处于维护模式,被使用 systemd 的 Ubuntu抛弃。应该查看 systemd 的手册以了解如何编写服务定义的详细信息。

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.

由于您使用的是 Ubuntu,您可能对Upstart感兴趣,它取代了传统的sysV init。一个关键特性是,如果服务意外终止,它可以重新启动服务。Fedora 已成为新贵,而 Debian 处于试验阶段,因此可能值得研究。

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

不过,对于这种情况,这可能有点过头了,因为执行cron脚本需要 2 分钟。

#!/bin/bash
if [[ ! `pidof -s yourapp` ]]; then
    invoke-rc.d yourapp start
fi

回答by Gareth

Monitis perfect for this :)

Monit非常适合这个 :)

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

您可以编写简单的配置文件,告诉 monit 监视例如 TCP 端口、PID 文件等

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.

monit 将运行您指定的命令,当它正在监视的进程不可用/使用太多内存/挂住 CPU 时间过长/等等。它还会弹出一封电子邮件警报,告诉您发生了什么以及它是否可以对此采取任何措施。

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

-- 您忠实的员工,Monit

回答by Javier

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.

这是 DMD(守护进程监控守护进程)的工作。周围有几个;但我通常只写一个脚本来检查守护进程是否正在运行,如果没有运行,然后将它放在 cron 中每分钟运行一次。

回答by gx.

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.

首先,你如何启动这个应用程序?它是否将自己分叉到后台?它是从 nohup .. & 等开始的吗?如果是后者,在 nohup.out 中查看为什么会死掉,如果是第一种,则构建日志记录。

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:

至于您的主要问题:您可以对其进行 cron,或者在后台运行另一个进程(不是最佳选择)并在 bashscript 中使用 pidof,这很简单:

if [ `pidof -s app` -eq 0 ]; then
    nohup app &
fi

回答by Klathzazt

Put your run in a loop- so when it exits, it runs again... while(true){ run my app.. }

将您的运行置于循环中 - 因此当它退出时,它会再次运行... while(true){ run my app.. }

回答by Paul Tomblin

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.

您可以将其设置为从 inittab 启动的服务(尽管某些 Linux 已转移到 /etc/event.d 中的更新内容)。这些内置系统可确保您的服务保持运行,而无需编写自己的脚本或安装新的东西。

回答by Jonathan Leffler

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

查看“Unix Hater's Handbook”(该书的 PDF 格式之一nanny)的第 9 章(p197 或相关内容)中引用的“ ”

回答by JimB

I have used from cron "killall -0 programname || /etc/init.d/programname start". kill will error if the process doesn't exist. If it does exist, it'll deliver a null signal to the process (which the kernel will ignore and not bother passing on.)

我从 cron 中使用了“killall -0 程序名 || /etc/init.d/programname start”。如果进程不存在,kill 会出错。如果它确实存在,它将向进程传递一个空信号(内核将忽略它并且不会打扰传递。)

This idiom is simple to remember (IMHO). Generally I use this while I'm still trying to discover why the service itself is failing. IMHO a program shouldn't just disappear unexpectedly :)

这个习语很容易记住(恕我直言)。通常我会在我仍在尝试发现服务本身失败的原因时使用它。恕我直言,程序不应该只是意外消失:)

回答by JimB

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.

我在 cron 中使用了一个简单的脚本来确保程序正在运行。如果不是,那么它将启动它。这可能不是您正在寻找的完美解决方案,但它很简单,而且效果很好。

#!/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.

然后每分钟或每 5 分钟添加一个 cron 作业。

回答by Casey Rodarmor

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
  1. 如果您的服务器无法侦听预期的端口,则将其写入死机
  2. 设置一个 cronjob 尝试每分钟启动你的服务器

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.

如果它没有运行,它就会启动,如果它正在运行,它就不会启动。无论如何,您的服务器将始终处于运行状态。