Basically the title. I've noticed recently that some of our processes are hitting an error during server start up which causes them to stop, and which pm2 does not restart them from automatically. Running pm2 restart \process name\
brings them back. As a stop gap measure while I investigate the root cause, I would like to setup a script to use pm2 restart on only stopped processes, to be used on server reboot. So far, I've been able to extract a list of the stopped processes and there status:
pm2 ls | grep "stopped" | grep "process"
However, after this, I'm not sure how to proceed with using pm2 restart on the processes. Will I have to extract the process names into a list and loop through it, or is there a better way to do this?
You can use the next command line:
pm2 ls | grep "stopped" | grep "process" | awk '{print $4}' | xargs -I{} pm2 start {}
pm2 ls
gets the list of all processes.
grep "stopped" | grep "process"
filters all lines that has stopped word and the all lines that has process word.
awk '{print $4}'
gets the fourth column per line, having that the format for each line this:
| 20 | process_name | ...
^ ^ ^ ^ ^
1 2 3 4 5 ...
xargs -I{} pm2 start {}
gets each result line and pass them to pm2 start
command.