进程终止时自动重新启动进程?

Ole*_*Ole 1 process monitoring nohup background-process raspberry-pi

我有一个在树莓派上运行的进程。在 ssh 进入进程后,像这样启动:

nohup .../blah/blah &
Run Code Online (Sandbox Code Playgroud)

IIUC 这允许我注销 pi 并且进程继续运行。但是它有时会死掉,我必须登录并手动重新启动它。有没有办法监控它并让它自己重新启动?

Kus*_*nda 6

在无限循环中运行它:

#!/bin/sh

while true; do
    .../blah/blah
done
Run Code Online (Sandbox Code Playgroud)

这将是您nohup在后台开始的脚本。当blah死掉时,它会立即重新启动,直到脚本被杀死。

如果调用的文件stopme出现在脚本的工作目录中,则结束循环的变体(仅在 (re-)starting 之前进行检查blah):

#!/bin/sh

while true; do
    [ -e stopme ] && break
    .../blah/blah
done
Run Code Online (Sandbox Code Playgroud)