简体   繁体   English

自动终止时如何重新启动任一进程

[英]How to restart either process when it kills automatically

i have 2 processes which needs to run simultaneously (process1 and process2-same names).我有 2 个需要同时运行的进程(进程 1 和进程 2 名称相同)。

i wrote a script which will automatically restarts all the 2 processes if those 2 killed.我写了一个脚本,如果这 2 个进程被杀死,它将自动重新启动所有 2 个进程。 but i am stuck at the point where if either of the process (anyone) is killed ,then the particular process not restarting.但我被困在如果其中一个进程(任何人)被杀死,那么特定进程不会重新启动。

#!/bin/bash

cd /Desktop/XYZ/

./process1 &

cd /Desktop/XYX/new/

./process1 &

while :
do

while pgrep process1 >/Desktop/XYZ/out.txt;

do

   sleep 5

done
    cd /Desktop/XYZ/

    ./process1 &

   cd /Desktop/XYX/new/

./process1 &

   sleep 10
done

any help will be greatly appreciated.任何帮助将不胜感激。

Just start the monitoring code too in their own subprocesses.只需在他们自己的子进程中启动监控代码。

#!/bin/bash

while :; do
    cd /Desktop/XYZ/
    ./process1
done &

while :; do
    cd /Desktop/XYX/new
    ./process1
done &

read  # halt until it gets input
pkill -P $$  # kill all children

I have to add that this is probably not how you want to run your services longterm.我必须补充一点,这可能不是您希望长期运行服务的方式。 You should look into how systemd service files can do this for you, or any other service manager that your system has.您应该研究systemd 服务文件如何为您或您的系统拥有的任何其他服务管理器执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM