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

时间:2022-02-11 08:04:22

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.

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

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.

我正在考虑使用cron作业检查每一分钟它是否还活着,但可能有一种更清洁的方式或标准方法。

Thanks in advance

提前致谢

8 个解决方案

#1


9  

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

Fedora和Ubuntu使用upstart,它可以在退出时自动重启你的守护程序。

#2


6  

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

#3


4  

Monit can do what you want and much more.

Monit可以做你想做的事情等等。

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.

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

#4


4  

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

在* 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.

    使用OS基础结构(如solaris上的smf / svc,Ubuntu上的upstart等)。这是正确的方法,因为您可以随时停止/重启/启用/禁用/重新配置。

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

    在/ etc / inittab中使用“respawn”(在引导时启用)。

#5


2  

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.

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

#6


1  

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)

创建您希望作为“观察者”进程的子进程连续运行的程序,该进程在终止时重新启动它。您可以使用wait / waitpid(或SIGCHILD)来判断子进程何时终止。我希望有人编写代码来执行此操作(这几乎是init(8)所做的)

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:

编辑:您可以让init为您执行此操作 - 在inittab中为其指定一种“respawn”。从手册页:

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

#7


1  

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.

如果脚本每10分钟检查一次,看看应用程序是否正在运行,如果不是,它将重新启动计算机。如果应用程序正在运行,那么它只是继续检查。

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.

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

@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

@echo off PATH =%PATH%;%PROGRAMFILES%\ PV;%PROGRAMFILES%\ Notepad PV.EXE notepad.exe> nul if ERRORLEVEL 1 goto Process_NotFound:Process_Found echo Notepad正在运行goto END:Process_NotFound echo Notepad未运行shutdown / r / t 50转到END:END

#8


0  

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?

这并不容易。如果您正在考虑“我知道,我会编写一个程序来监视我的程序,或者查看这样的程序是否已作为标准服务存在!”如果有人杀了那个节目怎么办?谁看守望者?

#1


9  

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

Fedora和Ubuntu使用upstart,它可以在退出时自动重启你的守护程序。

#2


6  

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

#3


4  

Monit can do what you want and much more.

Monit可以做你想做的事情等等。

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.

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

#4


4  

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

在* 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.

    使用OS基础结构(如solaris上的smf / svc,Ubuntu上的upstart等)。这是正确的方法,因为您可以随时停止/重启/启用/禁用/重新配置。

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

    在/ etc / inittab中使用“respawn”(在引导时启用)。

#5


2  

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.

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

#6


1  

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)

创建您希望作为“观察者”进程的子进程连续运行的程序,该进程在终止时重新启动它。您可以使用wait / waitpid(或SIGCHILD)来判断子进程何时终止。我希望有人编写代码来执行此操作(这几乎是init(8)所做的)

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:

编辑:您可以让init为您执行此操作 - 在inittab中为其指定一种“respawn”。从手册页:

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

#7


1  

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.

如果脚本每10分钟检查一次,看看应用程序是否正在运行,如果不是,它将重新启动计算机。如果应用程序正在运行,那么它只是继续检查。

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.

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

@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

@echo off PATH =%PATH%;%PROGRAMFILES%\ PV;%PROGRAMFILES%\ Notepad PV.EXE notepad.exe> nul if ERRORLEVEL 1 goto Process_NotFound:Process_Found echo Notepad正在运行goto END:Process_NotFound echo Notepad未运行shutdown / r / t 50转到END:END

#8


0  

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?

这并不容易。如果您正在考虑“我知道,我会编写一个程序来监视我的程序,或者查看这样的程序是否已作为标准服务存在!”如果有人杀了那个节目怎么办?谁看守望者?