Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of PHP Scripts in Bash script executed by Cron not running

I have four PHP scripts that perform various tasks that need to be executed sequentially.

Instead of creating a cron entry for each PHP script, I thought I would be clever and wrap it up as a Bash script as below:

#!/bin/bash

# set notification email.
NOTIFYEMAIL="[email protected]"

# set PHP path.
PHP="$(which php)"

# set folder path.
FOLDER="/var/www/example.com/processors/"

# list of scripts.
SCRIPTS=("script_1" "script_2" "script_3" "script_4")

# execute each script.
for i in "${SCRIPTS[@]}"
do
    SCRIPT="$FOLDER/$i.php"
    printf "Processing: %s\n" $i
    chmod +x $SCRIPT
    $PHP $SCRIPT
done

echo "Scripts have been processed." | mail -s "Scripts Processed" $NOTIFYEMAIL

I've tested all the scripts individually, and they work without error, outputting processing information to the terminal.

/usr/bin/php /var/www/example.com/processors/script_1.php

I've even tested the bash script which calls the four PHP scripts in sequence, manually via the terminal and that also works without error.

/bin/bash /var/www/example.com/processors/run.sh

But for some reason, the PHP scripts don't execute when I leave cron to run them using the following on the root users crontab.

# process daily.
0 1 * * * /bin/bash /var/www/example.com/processors/run.sh > /dev/null

I know that the crontab has been processed and the Bash script has run because I always receive the notification e-mail at the end of the script, each and every time.

Permissions are as follows:

-rwxr-xr-x 1 root root     510 Mar 11 09:21 run.sh
-rwxrwxr-x 1 git git      2.8K Mar  8 10:17 script_1.php
-rwxrwxr-x 1 git git      2.6K Mar  1 18:07 script_2.php
-rwxrwxr-x 1 git git       717 Mar  1 18:07 script_3.php
-rwxrwxr-x 1 git git      6.8K Mar  4 16:30 script_4.php

There are no cron-specific log files such as /var/log/cron.log but when I run grep CRON /var/log/syslog and it returns the following generic output:

Mar 11 06:39:01 s00000000 CRON[11651]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)
Mar 11 07:09:01 s00000000 CRON[12771]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete)
Mar 11 07:17:01 s00000000 CRON[13074]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

Any suggestions for things for me to try would be appreciated.

like image 975
paperclip Avatar asked Nov 03 '22 02:11

paperclip


1 Answers

Preliminary

  • Check that in the crontab the values of SHELL and PATH are set up correctly, e.g.:

    SHELL=/bin/bash
    PATH=/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/local/sbin:/usr/sbin
    
    • Ensure that the intended shell (e.g. bash) is really used.
    • Ensure that all needed binaries are accessible via the denoted PATH.
  • Check that you don't have an unescaped % in the crontab. See man 5 crontab for details.
  • Maybe you set also the environment variable MAILTO to an approbate value, e.g.:

    MAILTO="[email protected]"
    

Ensure your crontab is working

Another little trick to ensure that your crontab is parsed correct, is to add a marker entry at the end.

I'm using myself this marker entry

@daily logger -p local0.notice -t CRON '$Id: demo 7029 2012-11-27 09:27:39Z dirk $'

If the marker entry is missing in the syslog, you know that the crontab has become corrupted. This may be triggered by some weird syntax error you haven't seen.

How to debug a cron job

If a cron job it not working please investigate first the job output.

Change your crontab to:

0 1 * * * /bin/bash -x /var/www/example.com/processors/run.sh >>/tmp/cronTrace 2>&1

Please investigate the generated script output together with the trace output (bash -x).

Additional note

chmod +x $SCRIPT
$PHP $SCRIPT

The chmod step is not necessary and should be avoided. In the next line you invoke the script by explicitly use the interpreter command.

like image 152
H.-Dirk Schmitt Avatar answered Nov 05 '22 15:11

H.-Dirk Schmitt