0

Update: I was not able to get this working as I desired. I ended up using the pm2 --cron flag since cron couldn't find my pm2 processes.

I am using pm2 restart in a bash script and it always fails on the pm2 restart projectName commands when run by cron (as root) but works when run manually with sudo. I am not sure how to troubleshoot as the pm2 log files don't show anything obvious. It looks like someone else has had the same issue so perhaps it's just a bug? Has anybody else found a way to resolve this issue?

Thank you!

Edit: Adding some context for @kevinnls

Here's the .sh script; I've isolated the command so I can test cron

# Vars
NOW=$(date +"%Y-%m-%d")
LOGS_PATH="/scriptLoc/logs"
LOG_FILE="$LOGS_PATH/$NOW-log.txt"
BE_PATH="/beLoc"

# Start log file
date >> $LOG_FILE
echo "Running..." >> $LOG_FILE

# Temp
cd $BE_PATH
pm2 restart be >> $LOG_FILE
pm2 restart be || echo "ERROR: BE pm2 restart: $?" >> $LOG_FILE
echo "Done." >> $LOG_FILE
exit
  • If I run the command with sudo ./script.sh it works
  • If I run it with cron I see the following output in my log file:
Fri Mar 26 17:35:01 UTC 2021
Running...
Use --update-env to update environment variables
ERROR: BE pm2 restart: 1
Done.

If I view pm2 logs:

  • I see it exit with code 0 and restart when I run the script manually.
  • I see no output for the failed restart from cron
Bix
  • 760
  • 8
  • 22
  • have you tried logging the output of `pm2 restart` to a file or checking `mail` (if it's configured)? what errors does it throw? – kevinnls Mar 26 '21 at 17:24
  • I updated my post to provide some more context. I don't have mail configured. Is there another way I can log the output of `pm2 restart` ? – Bix Mar 26 '21 at 17:44
  • what you are doing is +1, but to include error messsage into the log, make this change ```#redirect output and errors to log pm2 restart be &>> $LOG_FILE # if previous exit code != 0 do everything after `&&' [[ $? -ne 0 ]] && echo "ERROR: BE pm2 restart: $?" >> $LOG_FILE``` Okay comments do NOT work for code. – kevinnls Mar 26 '21 at 17:48

2 Answers2

1

The error you got:

[PM2][ERROR] Process or Namespace be not found

What is "be"? seems like pm2 from inside of cron has no idea what to make of it.

If it is a file, when you ran the script in your shell, was the file be in your working directory?


You can make these changes to log your error messages to $LOG_FILE.
This should help you get more details.

#redirect output *and* errors to log 
pm2 restart be &>> $LOG_FILE
exit_code=$?
# if previous exit code != 0 do everything after `&&' 
[[ $exit_code -ne 0 ]] && echo "ERROR: BE pm2 restart: $exit_code" >> $LOG_FILE

Then try running your script again and share the errors encountered.

kevinnls
  • 728
  • 4
  • 19
  • the error is likely to do with some environment variable required by pm2 not being available in the cron environment. it's a rather common hitch people face. that would also explain why it works fine in your shell. – kevinnls Mar 26 '21 at 18:00
  • `[PM2][ERROR] Process or Namespace be not found` so thinking I'll just stop and start it instead of restarting; going to try that now – Bix Mar 26 '21 at 18:07
  • be is the process name. I started it with `pm2 start start.js --name be`. I also just tried `pm2 restart 1` using it's process id (not found) as well as `pm2 restart all` (no process found). I also just tried having cron itself [run the restart command](https://stackoverflow.com/questions/37514444/restart-pm2-app-every-12h-with-cron) but that also failed. Last thing I'm going to attempt is using the cron flag on pm2 itself. – Bix Mar 26 '21 at 18:28
  • are you certain about the process being owned by root? – kevinnls Mar 26 '21 at 18:55
  • Fairly certain - I'm running `sudo pm2 start` and `sudo crontab -e`. I was able to get it working with the pm2 --cron flag so I'm just going to use that for now. I submitted a bug report to pm2 so I'll update this if I get a response. Going to approve your answer, I appreciate all your help @kevinnls – Bix Mar 26 '21 at 19:12
  • update the log your question with the error message – kevinnls Mar 27 '21 at 07:13
  • let's continue the troubleshooting on [chat.](https://chat.stackoverflow.com/rooms/230433/discussion-between-kevinnls-and-bix) i tried to recreate your problem, but couldn't – kevinnls Mar 27 '21 at 09:11
0

I was not able to resolve this in the manner I desired. Ultimately I used the -c (cron) flag with pm2 itself.

pm2 cli reference

gitlab issue

Bix
  • 760
  • 8
  • 22