spirytus
spirytus

Reputation: 10946

Simple script run via cronjob doesn't work but works from shell

I am on shared hosting and I'm trying to schedule cronjob to run every now and then. Via cPanel I scheduled to execute my script but even though that according to my host support the cronjob runs, the script doesn't seem as doing anything. The cron job command I set via cPanel is:

/bin/sh /home1/myusername/public_html/somefolder/cronjob2.sh

and the cronjob2.sh

#!/bin/bash
/home1/myusername/public_html/somefolder/node_modules/forever/bin/forever stop 0

when via SSH I execute:

/home1/myusername/public_html/somefolder/cronjob2.sh

it stops forever process as needed. From cronjob doesn't do anything.

How can I get this working?

EDIT:

So I've tried:

/bin/sh /home1/username/public_html/somefolder/cronjob2.sh >> /tmp/mylog 2>&1

and mylog entries say:

/usr/bin/env: node: No such file or directory

It seems that forever needs to run node and this cannot be found. How would I possibly fix this?

EDIT2:

Accepted answer at superuser.com. Thank you all for help https://superuser.com/questions/763261/simple-script-run-via-cronjob-doesnt-work-but-works-from-shell/763288#763288

Upvotes: 1

Views: 4869

Answers (2)

Mart Rivilis
Mart Rivilis

Reputation: 122

For cron job lines in a crontab it's not required to specify kind of shell or e.g. of perl. It's enough, that your script contains shebang line. Therefore you should remove /bin/sh from your cron job line.

Another aspect, that might cause a different behavior of your script by interactive start and by cron daemon start is possible different environment, first of all the PATH variable. Therefore check, if you script is able to be executed in very restricted environment, that is provided by cron daemon. You can determine your cron job environment experimentally by start of temporary cron job, that executes "env" command and writes its output to a file.

Once more aspect: Have you redirected STDOUT and STDERR of the cron job to a log file and read its content to analyze the issue? You can do it as follows:

 your_cron_job   >/tmp/any_name.log  2>&1

Upvotes: 1

julienc
julienc

Reputation: 20325

According to what you wrote, when you run your script via SSH, you are using bash, because this line is the first of your script:

#!/bin/bash

However, in the crontab, you are forcing the use of sh instead of bash. Are you sure your script is fully compatible with sh? Otherwise, simply replace /bin/sh with /bin/bash in your cron command and test again.

Upvotes: 0

Related Questions