2

I am trying to execute a php file via cron every 2 minutes, but it's not working. My crontab is:

 */2 * * * * usr/bin/php /var/www/test/cron.php

What am I doing wrong?

  • 4
    Leading / for the path: /usr/bin/php – muru Apr 29 '15 at 12:39
  • */2 * * * * /usr/bin/php /var/www/test/cron.php AND */2 * * * * /usr/bin/php -q /var/www/test/cron.php both of them not working. – Mahesh Gareja Apr 30 '15 at 5:14
  • thank you so much. now it's working properly . i was wrong @path mapping. – Mahesh Gareja May 1 '15 at 4:12
1

Check the output of which php and use the absolute path for php (in my case /usr/bin/php5).

*/2 * * * * /usr/bin/php /var/www/test/cron.php

or just

*/2 * * * * php /var/www/test/cron.php

For clarification, the default $PATH for cron is

PATH=/usr/bin:/bin

You can check the $PATH with a test entry (Source):

* * * * * env > /tmp/env.output

Thus, the file

/tmp/env.output

is created.

You'll have to remove the entry * * * * * env > /tmp/env.output afterwards.

  • i don't understand. i have to define path inside the crontab? PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/b‌​in */2 * * * * /usr/bin/php /var/www/test/cron.php like this ? – Mahesh Gareja Apr 30 '15 at 5:25
  • No, you need */2 * * * * /usr/bin/php /var/www/test/cron.php if your php interpreter can be found here /usr/bin/php – A.B. Apr 30 '15 at 5:40
  • thank you so much. now it's working properly . i was wrong @path mapping. – Mahesh Gareja May 1 '15 at 4:12
  • kindly, i am new here and this is my first post so i am not aware of rules in regulation in detail. again thanks for help. – Mahesh Gareja May 1 '15 at 7:34
2

My recommendation would be to put call the script using standard web path, so you don't mingle the users and permissions, e.g. instead of doing:

/usr/bin/php <script>

rather do:

/usr/bin/wget -q http://localhost/test/cron.php

Then you need to make sure the script can be called just from localhost (f.e. using Apache2 access policy).

This way the cron script will be always run under the same user as the website which is a good policy to have in place.

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.