Crontab running bash script logs "command not found"

I'm trying to run a command at certain intervals.

pm2 restart appname

Doing this myself works. Then I add it to a bash script.

#!/bin/bash
pm2 restart appname

Running the bash script works: bash scriptname.sh

Then I add it to my crontab:

* * * * * /usr/local/crontab-scripts/scriptname.sh >> /var/log/cronlog.log 2>&1

This gives me the error:/usr/local/crontab-scripts/scriptname.sh: line 4: pm2: command not found

I'm not doing any of this as root, I have pm2 globally installed, I put into my script whereis pm2 and it logged the correct path /usr/local/bin/pm2. Then I tried having my script cd to that path before running pm2, no difference.

I'm out of ideas here.

0

1 Answer

When you call applications or commands from within scripts or CRON jobs, it helps to spell out the entire path of the application.

In your case I would try re-writing your script to:

#!/bin/bash
/usr/local/bin/pm2 restart appname

However, this seems like overkill and you might as well just call pm2 directly from your crontab:

* * * * * /usr/local/bin/pm2 restart appname >> /var/log/cronlog.log 2>&1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like