开发者

Monitor a process [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

开发者_如何学编程

Closed 5 years ago.

Improve this question

Is there a way I a process can be monitor and if it were to die, can a script be called which in turn will bring the process back up ?


#!/bin/bash
while true
do
  if pgrep myprocess  >/dev/null ;then
     echo "up"
  else
     echo "down"
     /path/to/myprocess &
  fi
  sleep 60
done

or you could remove the while loop and sleep and put the script in a cron job set to run every minute


monit.

http://mmonit.com/monit/


I wrote one of these a while ago called relight. There also exist more robust solutions, but this one is simple and works for me.


The easiest thing to do is have the initial parent do the monitoring. EG,

#!/bin/sh

while true; do
  cmd
  # When you get here the process has died.  start
  # the loop again and restart it
done

This script is liable to be killed so you might want to trap signals, but the same will be true of any monitor that you might write. You will probably also want to insert a delay if cmd is terminating immediately, or add some logging (call logger after you call cmd). There's no need to get fancy.


Using a command that you specify in a simple configuration file, Supervisor can start, monitor, and restart a process that unexpectedly dies.

Consider the following Supervisor configuration file fragment in /etc/supervisor/conf.d/forever.conf which displays the date and time every second:

[program:forever]
command=/bin/bash -c 'while true; do echo "Current time is `date`"; sleep 1; done;'

Program forever starts with PID 15474:

derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever
forever                          RUNNING   pid 15474, uptime 0:00:17
derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever
Current time is Fri Jul  7 17:11:10 EDT 2017
Current time is Fri Jul  7 17:11:11 EDT 2017
Current time is Fri Jul  7 17:11:12 EDT 2017
Current time is Fri Jul  7 17:11:13 EDT 2017
Current time is Fri Jul  7 17:11:14 EDT 2017
Current time is Fri Jul  7 17:11:15 EDT 2017
Current time is Fri Jul  7 17:11:16 EDT 2017
Current time is Fri Jul  7 17:11:17 EDT 2017
Current time is Fri Jul  7 17:11:18 EDT 2017
Current time is Fri Jul  7 17:11:19 EDT 2017
Current time is Fri Jul  7 17:11:20 EDT 2017
Current time is Fri Jul  7 17:11:21 EDT 2017
Current time is Fri Jul  7 17:11:22 EDT 2017
Current time is Fri Jul  7 17:11:23 EDT 2017
Current time is Fri Jul  7 17:11:24 EDT 2017
Current time is Fri Jul  7 17:11:25 EDT 2017

Kill the forever process and Supervisor restarts it automatically with new process ID 15760:

derek@derek-lubuntu:~/Projects/fire$ sudo kill 15474
derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever
forever                          RUNNING   pid 15760, uptime 0:00:02
derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever
Current time is Fri Jul  7 17:11:21 EDT 2017
Current time is Fri Jul  7 17:11:22 EDT 2017
Current time is Fri Jul  7 17:11:23 EDT 2017
Current time is Fri Jul  7 17:11:24 EDT 2017
Current time is Fri Jul  7 17:11:25 EDT 2017
Current time is Fri Jul  7 17:11:26 EDT 2017
Current time is Fri Jul  7 17:11:27 EDT 2017
Current time is Fri Jul  7 17:11:28 EDT 2017
Current time is Fri Jul  7 17:11:29 EDT 2017
Current time is Fri Jul  7 17:11:30 EDT 2017
Current time is Fri Jul  7 17:11:31 EDT 2017
Current time is Fri Jul  7 17:11:32 EDT 2017
Current time is Fri Jul  7 17:11:33 EDT 2017
Current time is Fri Jul  7 17:11:34 EDT 2017
Current time is Fri Jul  7 17:11:35 EDT 2017
Current time is Fri Jul  7 17:11:36 EDT 2017


If you are using SysV system (not Upstart), you can put the process do respawn at inittab.

Just edit your /etc/inittab file and add a line like this:

proc:12345:respawn:/path/to/process


There are number of ways of getting the task done:

  1. As suggested by others - Run a script to check if process is running, restart the process if not running. To check you if process is running or not you can use pgrep <process name> | wc -l
  2. Use watch command to run a script after some interval to check if process is running, if not then restart the process
  3. Create a parent process, that will always look for the child process, if child process crashes or stops, parent will be notified, then restarts new process.


systemd is a sophisticated process manager available on most major Linux distributions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜