I have a PHP scheduler script I need apache to execute on a cron schedule. Testing this command from command line works fine:

$    ! test -e /var/www/html/maintenance.flag && cd /var/www/html/shell && /bin/php ./scheduler.php --action cron --mode default

When I test this script using the command shown below as my apache user or add it to the apache cron I get this error: Could not open input file: ./scheduler.php

$    sudo -H -u apache bash -c "! test -e /var/www/html/maintenance.flag && cd /var/www/html/shell &&  /bin/php ./scheduler.php --action cron --mode default"

Note that scheduler.php is a symlink with read/write/execute on the on the link and source file (chmod ug+rwx scheduler.php ./vendor/vendor/package/src/scheduler.php).

I originally tried simply running php /var/www/html/shell/scheduler.php --action cron --mode default, however the PHP script references files in the same directory which is why I need apache/cron to execute the file from within that directory.


Tried using a script file but am still seeing the same error.

Created a cron.sh with the following content:

cd /var/www/html/shell

#For Testing - remember, this is a symlink file
ls -l 'scheduler.php'

#Run Scheduler
/bin/php ./scheduler.php --action cron --mode always
/bin/php ./scheduler.php --action cron --mode default

Tested with:

sudo -H -u apache bash -c "! test -e /var/www/html/maintenance.flag && /bin/bash /var/www/html/cron.sh"


lrwxrwxrwx 1 apache webroot 53 Oct 26 10:36 scheduler.php -> ../vendor/aoepeople/aoe_scheduler/shell/scheduler.php
Could not open input file: scheduler.php
Could not open input file: scheduler.php
  • Use full paths for everything in the cron and you should be set. And use absolute paths inside your script if necessary. – Videonauth Dec 8 '16 at 19:15
  • ls -lL 'scheduler.php'. Does the user you're running as have r-x on the scheduler.php file and at least r-- on each of the directories in ../vendor/aoepeople/aoe_scheduler/shell/ ? – waltinator Dec 8 '16 at 19:51
  • Yes, the output of $ ls -lL scheduler.php is -rwxrwxr-x 1 centos apache 17359 Oct 26 10:35 scheduler.php – TylersSN Dec 8 '16 at 19:56
  • The same goes for the directory ./vendor/aoepeople/aoe_scheduler/shell – TylersSN Dec 8 '16 at 19:57
  • Please add the output of namei -lx /var/www/html/shell/scheduler.php – muru Dec 9 '16 at 1:38

Each of the sub expressions to && (test...,cd ... and /bin/php...) execute in different subshells, and cannot pass context (like $PWD) among themselves.

Rather than put your command in your crontab, wrap it in a simple bash script, and call that script from crontab. E.g.:
In crontab:

! test -e /var/www/html/maintenance.flag && /usr/local/bin/maint

and, in /usr/local/bin/maint:

cd /var/www/html/shell
/bin/php ./scheduler.php --action cron --mode default"
  • Thank you for your suggestion. Please see my updated post. – TylersSN Dec 8 '16 at 19:48
  • @waltinator the statement about each subexpression to && having its own subshell is not true. To verify, run a simple command like cd ~/ && echo $PWD && cd /tmp && echo $PWD – Jakub Kukul Jun 28 '20 at 15:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.