Cron's PHP script sequence in Bash script does not work

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

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

#!/bin/bash

# set notification email.
NOTIFYEMAIL="webmaster@example.com"

# 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 tested all the scripts separately, and they work without errors, displaying information about the processing on the terminal.

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

I even checked the Bash script, which sequentially calls four PHP scripts, manually through the terminal and also works without errors.

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

But for some reason, PHP scripts are not executed when I leave cron to run them using the following in the root of crontab.

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

, crontab Bash script , script .

:

-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

, cron, /var/log/cron.log, grep CRON /var/log/syslog :

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)

, , .

+5
1

  • , crontab SHELL PATH , :

    SHELL=/bin/bash
    PATH=/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/local/sbin:/usr/sbin
    
    • , (, bash) .
    • , PATH.
  • , crontab unescaped %. . man 5 crontab.
  • , MAILTO , :

    MAILTO="webmaster@example.com"
    

, crontab

, , crontab , .

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

syslog, , crontab . - , .

cron

cron , .

crontab :

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

, script (bash -x).

chmod +x $SCRIPTp >
$PHP $SCRIPT

chmod . script, .

+1

All Articles