22

I have built one php file to check some result, so that I need to setup a cronjob.

I set one to run every 30 minute, so that the results will be send. However, I don't know why my crontab did not run after every 30 minute.

Here is how I set the crontab:

*/30 * * * * php /var/www/html/result.php

I have confirmed my file directory is correct. What I not sure is about the timing part: isn't it possible to use */30 * * * * or 30 * * * * ? I set */30 * * * * and did not work.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user941885
  • 223
  • 1
  • 3
  • 5
  • `*/30 * * * *` will execute every 30 minutes, whereas `30 * * * *` will execute on the 30th minute of every hour. Here's a [helpful link]('http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/') you can easily google for more. – Danny Bullis Jun 22 '12 at 18:43
  • Also, try running `php -f /var/www/html/result.php` directly from the command line and see what kind of output you get; if your output shows a fatal error, your issue is likely with your PHP script. – Danny Bullis Jun 22 '12 at 18:44

15 Answers15

72

Given

*/30 * * * * php /var/www/html/result.php

There are multiple possibilities why it is not working:

  1. First of all it is important to check if the simple execution of php /var/www/html/result.php. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.

  2. The path of the php binary has to be added.

    */30 * * * * php /var/www/html/result.php
    

    to be changed to

    */30 * * * * /usr/bin/php /var/www/html/result.php
    

    or whatever coming from which php.

  3. Check the permission of the script to the user running the crontab.

    Give execution permission to the file: chmod +x file. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.

  4. To be safer, you can also add the php path in the top of the script, such as:

    #!/usr/bin/php -q
    <?php
    
     ...
    
    ?>
    
  5. Make sure the user has rights to use crontab. Check if he is in the /etc/cron.d/deny file. Also, make a basic test to see if it is a crontanb or php problem.

    * * * * * touch /tmp/hello
    
  6. Output the result of the script to a log file, as William Niu suggested.

    */30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
    
  7. Use the -f option to execute the script:

    */30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
    
  8. Make sure the format in crontab is correct. You can do so for example using the site Crontab.guru.


To sum up, there are many possible reasons. One of them should solve the problem.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • None of them worked out with me. Changed the permission to 744 for the file but without luck :( – moderns Apr 07 '14 at 00:52
  • You can also check the "Debugging crontab" section in http://stackoverflow.com/tags/crontab/info – fedorqui Apr 07 '14 at 08:15
  • 3
    Another variation, perhaps #8, could be due to dependencies inside of the .php file (it'll pass all the #1-#7 but still not work). Example: include 'config.inc.php'; One way to fix: 30 2 * * * cd /path/to/phpfiles/; /usr/bin/php -q script.php – user1652110 Nov 24 '16 at 03:57
  • 2
    To me, it was the file path name. That is, when I used "test.log" in the php program, it didn't work. However, after I fixed it into a full path of the file in the script it worked. "/home/whoever/test/test.log" – Park JongBum Apr 03 '18 at 02:48
  • @ParkJongBum Thanks a lot!! I can run my php Script thanks to your comments. – Ale DC Feb 23 '23 at 01:14
10

It may be because php is not in the path. crontab has a very minimal path. So, include the full path for your php program.

you can test your cron commands by piping the output to a file, e.g.

*/30 * * * * php /var/www/html/result.php > /tmp/result.log

From this reference page, under "Crontab Environment":

cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh). cron supplies a default environment for every shell, defining:

HOME=user’s-home-directory 
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:. 
SHELL=/usr/bin/sh

Also, /30 syntax might not be supported by all platforms, so, try to change it to 0,30 instead.

William Niu
  • 15,798
  • 7
  • 53
  • 93
8

Had a similar issue; from command line, it worked, but from cron, no go.

had a "include ("./connect.php"); in my php code for the db stuff.

Removed that, and added the connect.php code directly into the php script, and it worked from cron.

Willem Abrie
  • 81
  • 1
  • 3
7

I had a similar issue on Ubuntu 14.04.1 and the problem turned out to be the way I was modifying the crontab:

I was using sudo crontab -e instead of just crontab -e and this caused my changes to be ignored.

nifo
  • 593
  • 2
  • 9
  • 21
  • 1
    This does not seem to make sense with my understanding of `sudo`. If anything, `sudo` should be one of the users who can do whatever it wants. – superuserdo Feb 02 '16 at 22:17
  • 1
    I think by appending sudo you select the root user (the cron settings for the root user) – galki Feb 14 '17 at 07:55
5

I had a funny one regarding this. Although my scripts would run manually, they wouldn't run from crontab.

Turns out that because the script was being run from /usr/bin/php rather that the location of the file (as it does when I run it manually) my php require wasn't finding the files I wanted. Changing that to reflect the full address fixed it.

troubleshooting by running the script as /usr/bin/php -f /var/www/myfile.php helped me find the issue

meewog
  • 1,690
  • 1
  • 22
  • 26
KingColours
  • 51
  • 1
  • 3
2

You can use */30 * * * * wget http://my.domain.com/path/to/php/result.php

But Crontab executes the task using the current user that ran crontab -e. When you use wget it’s handled by Apache using the www-data user/group pair

  1. First, make sure the script works as expected.

    $ php /var/www/html/result.php
    
  2. Second, edit the crontab for the Apache user account

    $ sudo crontab -u www-data -e
    

    or

    $ sudo crontab -u root -e
    
  3. Now add the crontab and output to a log file.

    */30 * * * * php /var/www/html/result.php > /tmp/result.log
    
Optimaz Prime
  • 857
  • 10
  • 11
1

After a day of puzzling why my script would work directly (to send data in an email to a gmail account) I discovered that all the deliberate sends worked when I clicked the url and all the cron sends went into spam. No idea why but I thought I'd share it.

1

I was stuck too. I am using centos 7 and had to run few php scripts. I initially tried this $crontab -e

& inserted the scripts to be executed at 12 midnight.

0 0 * * * usr/bin/php /var/www/html/cronjob/myscript.php

However in var /var/spool/mail/centos, it gave me an error in the mail there

/bin/sh: usr/bin/php: No such file or directory

So then I used wget like this,

$ crontab -e

0 0 * * * wget https://myapplicationurl/var/www/html/cronjob/myscript.php

This also gave me an error.

ERROR 404: Not Found.

Then I realized my mistake of specifying the folder, since the url will already be pointing to html folder, the folder from there i to be specified, like this

0 0 * * * wget http://myapplicationurl/cronjob/myscript.php

and it worked !!! Hope this helps any newbie like me :)

Supriya
  • 31
  • 3
1

if you php script has an include or require, you must provide the full path yours includes

wrong way

// relative path
require_once("../library/PHPMailer/PHPMailerAutoload.php");

Correct Way

// full path
require_once("/home/bitnami/library/PHPMailer/PHPMailerAutoload.php");
Ale DC
  • 1,646
  • 13
  • 20
0

Willem's answer showed me the way. In my case, I have a "include("connection.php")" inside my code. I changed connection.php to /my/full/path/connection.php. I have some rename() calls with the relative path, and I changed to the absolute path. That worked for me. I hope it can help someone else.

Paulo
  • 1,458
  • 2
  • 12
  • 26
0

Easy and logical way:

Checking the cron logs at /var/log/cron will give you very useful info

less /var/log/cron

Eg.,

My cron entry is * * * * * /usr/bin/php /cat.php <== Run cat.php every minute

The log file will contain an entry similar to the one below every time a cron entry is run

Jan 24 08:06:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)    
Jan 24 08:07:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)

Here, the php command will be executed every minute and there will be an entry in the log file every minute

If the entry is not there then crond is not even picking that cronjob. If the log entry is there and still you are not getting the desired output then there is something wrong with the command/application logic

Siva Prakash
  • 4,626
  • 34
  • 26
0

Are you sure it is not running? If you use exec, realize that you are running from cron and the full path for everything is required, so instead of cp, you'll need to use /bin/cp.

Trygve
  • 1,317
  • 10
  • 27
0

Centos 7

For the record (and it could work for other distros)

I had the next script

* * * * * /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt

But it failed to execute.

In the /var/log/cron log file, I found the next line

crond[2213]: (/usr/bin/php) ERROR (getpwnam() failed)

What is that?

It's simple, the syntax of corn is * * * * * user command (check user)

* * * * * someuser /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt
magallanes
  • 6,583
  • 4
  • 54
  • 55
0

Using Ubuntu w/ Vesta :

The following command works perfect for me,

/usr/bin/php /home/admin/web/mydomain.com/public_html/mycode.php

Feel free to comment if you have any question, have a nice day :)

Malcolm T
  • 41
  • 6
0

I had same problem with my php. Then I test execute php from root dir:

php -f /var/www/html/my_proj_folder/test.php

and got some errors regarding path to lib (included files), such as parse_ini with argument 'config.ini' <- has been taken from my global lib and lose path when it has been started from root. So,

  1. try to run your php-file (php -f your.file)
  2. check relative path and try to run with absolute path
  3. check permissions to your.php - it has to have executable flag x (you can see it ls -l your.php and set by chmod +x your.php)
  4. put #!/usr/bin/php -q before <?php in your main/executable file
Luss Sh
  • 21
  • 5