如何确保程序正在运行并在需要时重新启动它?

发布于 2024-07-18 02:56:47 字数 130 浏览 13 评论 0原文

我开发了一个需要持续运行的软件(用C++)。 这基本上意味着每次停止时都必须重新启动。

我正在考虑使用 cron 作业每分钟检查它是否仍然存在,但可能有一种更干净的方法或标准方法来执行此操作。

提前致谢

I developed a software (in C++) which needs to be continuously running. That basically means that it has to be restarted each time it stops.

I was thinking about using cron jobs to check every minutes if it is still alive, but there might be a cleaner way or standard way of doing this.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

温折酒 2024-07-25 02:56:48

Fedora 和 Ubuntu 使用 upstart,它能够在守护程序退出时自动重新启动它。

Fedora and Ubuntu use upstart, which has a the ability to automatically restart your deamon if it exits.

今天小雨转甜 2024-07-25 02:56:48

我相信最简单的方法是有一个脚本来启动你的程序,如果它返回它,只需重新启动它。

#!/bin/sh
while true; do
  ./Your_program
done

I believe the easiest way to do this is to have a script that will start your program and if it gets returned to it just restarts it.

#!/bin/sh
while true; do
  ./Your_program
done
浊酒尽余欢 2024-07-25 02:56:48

Monit 可以做你想做的事,甚至更多。

如果您的应用程序足够智能,可以检查自身运行情况(这是为了避免运行许多副本),那么 cron 是一个选项。 这通常是通过 PID 文件以标准方式完成的。

Monit can do what you want and much more.

cron is an option if your app will be smart enough to check for itself running (this is to avoid many copies of it running). This is usually done in a standard way via PID files.

是伱的 2024-07-25 02:56:48

在 *nix 上有两种正确的方法:

  1. 使用操作系统基础设施(如 Solaris 上的 smf/svc、Ubuntu 上的 upstart 等)。 这是正确的方法,因为您可以随时停止/重新启动/启用/禁用/重新配置。

  2. 在 /etc/inittab 中使用“respawn”(在启动时启用)。

There are two proper ways to do it on *nix:

  1. Use the OS infrastructure (like smf/svc on solaris, upstart on Ubuntu, etc...). This is the proper way as you can stop/restart/enable/disable/reconfigure at any time.

  2. Use "respawn" in /etc/inittab (enabled at boot time).

凌乱心跳 2024-07-25 02:56:48

launchtool 是我用于此目的的一个程序,它将监视您的进程并重新启动根据需要,它也可以在重新调用之前等待几秒钟。 如果在应用程序重新启动之前需要释放套接字,这会很有用。 这对我的目的非常有用。

launchtool is a program I used for this purpose, it will monitor your process and restart it as needed, it can also wait a few seconds before reinvocation. This can be useful in case there are sockets that need to be released before the app can start again. It was very useful for my purposes.

隔纱相望 2024-07-25 02:56:48

创建您希望作为“观察者”进程的子进程持续运行的程序,该进程在终止时会重新启动。 您可以使用 wait/waitpid (或 SIGCHILD)来告知子进程何时终止。 我希望有人编写的代码可以做到这一点(这几乎就是 init(8) 所做的)

但是,该程序可能会做一些事情。 您可能不仅想检查应用程序是否正在运行,还想检查它是否没有挂起或发生其他情况,并且正在提供其预期的服务。 这可能意味着运行某种探测或综合事务来检查其是否正常运行。

编辑:您也许可以让 init 为您执行此操作 - 在 inittab 中给它一种“重生”类型。 从手册页:

respawn
    The process will be restarted whenever it terminates (e.g. getty). 

Create the program you wish to have running continually as a child of a "watcher" process that re-starts it when it terminates. You can use wait/waitpid (or SIGCHILD) to tell when the child terminates. I would expect someone's written code to do this (it's pretty much what init(8) does)

However, the program presumably does something. You might want not only to check the application is running, but that it is not hung or something and is providing the service that it is intended to. This might mean running some sort of probe or synthetic transaction to check it's operating correctly.

EDIT: You may be able to get init to do this for you - give it a type of 'respawn' in inittab. From the man page:

respawn
    The process will be restarted whenever it terminates (e.g. getty). 
吃不饱 2024-07-25 02:56:48

大约每 10 分钟检查一次以查看应用程序是否正在运行的脚本怎么样,如果没有运行,它将重新启动计算机。 如果应用程序正在运行,那么它只会继续检查。

这是我使用 PrcView 的脚本,PrcView 是一个免费软件进程查看器实用程序。 我使用 notepad.exe 作为需要运行的示例应用程序,我不确定每 10 分钟检查一次的命令以及它在我的脚本中的位置。

@回声关闭
路径=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\记事本
PV.EXE记事本.exe>nul
如果错误级别 1 则转到 Process_NotFound
:进程_找到
echo 记事本正在运行
转到结束
:进程_未找到
echo 记事本未运行
关闭 /r /t 50
转到结束
:结尾

How about a script that check about every 10 minutes to see if the application is running and if it isn't it will restart the computer. If the application is running, then it just continues to check.

Here is my script using PrcView is a freeware process viewer utility. And I used notepad.exe as my example application that needs to be running, I am not sure the command for checking every 10 minutes and where it would go in my script.

@echo off
PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\Notepad
PV.EXE notepad.exe >nul
if ERRORLEVEL 1 goto Process_NotFound
:Process_Found
echo Notepad is running
goto END
:Process_NotFound
echo Notepad is not running
shutdown /r /t 50
goto END
:END

千纸鹤带着心事 2024-07-25 02:56:48

这并不容易。 如果您在想“我知道,我会编写一个程序来监视我的程序,或者看看这样的程序是否已经作为标准服务存在!” 那么如果有人杀死那个程序怎么办? 谁注视着观察者?

This is not so easy. If you're thinking "I know, I'll write a program to watch for my program, or see if such a program already exists as a standard service!" then what if someone kills that program? Who watches the watcher?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文