Process monitoring

Is there a way I can control the process, and if it has to die, can a script be called, which in turn will lead to the completion of the process?

+4
source share
8 answers
#!/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 can remove the while and sleep loop and put the script in the cron job task to run every minute

+6
source

I wrote one of them some time ago called relight . There are also more reliable solutions, but this one is simple and works for me.

+1
source

The easiest way is to have the initial parent monitor. 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 may be killed, so you may want to capture signals, but the same will be true for any monitor you can write. You probably also want to insert a delay if cmd exits immediately or add some logging (call log after cmd is called). No need to get imagination.

+1
source

If you use a SysV system (not Upstart), you can put the process back into inittab.

Just edit the / etc / inittab file and add the following line:

Proc: 12345: respawn: / path / to / process

0
source

There are several ways to accomplish a task:

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

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

0
source

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

Consider the following snippet of the Supervisor configuration file 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;' 

The forever program 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 will restart it automatically with the 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 
-1
source

Source: https://habr.com/ru/post/1302706/


All Articles