PM2:Return 已停止进程的列表并仅重新启动它们

PM2: Return list of stopped processes and restart only them

基本上是标题。我最近注意到我们的一些进程在服务器启动期间遇到错误,导致它们停止,而 pm2 不会自动重新启动它们。 运行 pm2 restart \process name\ 把他们带回来。作为我调查根本原因时的权宜之计,我想设置一个脚本以仅在停止的进程上使用 pm2 restart,以便在服务器重启时使用。到目前为止,我已经能够提取已停止进程的列表以及状态:

pm2 ls | grep "stopped" | grep "process"

但是,在此之后,我不确定如何继续在进程上使用 pm2 restart。我是否必须将进程名称提取到列表中并循环遍历它,还是有更好的方法来做到这一点?

可以使用下一个命令行:

pm2 ls | grep "stopped" | grep "process" | awk '{print }' | xargs -I{} pm2 start {}
  • pm2 ls 获取所有进程的列表。

  • grep "stopped" | grep "process" 过滤所有包含 stopped 单词的行和所有包含 process 单词的行.

  • awk '{print }' 获取每行的第四列,每行的格式为:

    | 20 | process_name | ...
    ^  ^ ^      ^       ^
    1  2 3      4       5 ...
    
  • xargs -I{} pm2 start {} 获取每个结果行并将它们传递给 pm2 start 命令。