-1

I want to add a cron job in my Raspberry to execute a task every five minutes. So I do in the terminal:

crontab -e

and then add at the file:

*/1 * * * * /usr/bin/php myscript path.

The script is something very simple. just to try if it works:

 <?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = date("l jS \of F Y h:i:s A") . "<br>";;
fwrite($myfile, $txt);
fclose($myfile);
?>

The problem is that the date isn't updated, so the cron job doesn't work. Any idea of the problem ?

UPDATE

This what I got, when I execute crobtab -e

    GNU nano 2.2.6        File: /tmp/crontab.3IXg0z/crontab                       

# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * /usr/bin/php /full/path/myscript.php
3
  • 1
    are you SURE it doesn't work? check your account's mail. cron emails output/job logs to the account.plus .../php myscript path implies that your php code file is called myscript, and you're passing in path as a command-line argument.
    – Marc B
    Apr 16, 2015 at 15:14
  • Can you go through the debugging steps on the crontab tag wiki and report your findings? Apr 16, 2015 at 15:25
  • The script has 777 permission, as the file. The full line, I have added is: */5 * * * * /usr/bin/php /var/www/mydomain.com/public_html/test.php Apr 16, 2015 at 15:37

3 Answers 3

3

First, make sure the script is executable:

chmod +x /path/to/some/script.php

Second, make sure your script has an appropriate #! (or "shebang") on the very first line:

#!/usr/bin/php

Then make sure your cron job is configured correctly. The format for cron is typically m h dom mon dow command

sudo crontab -e

*/5 * * * * /path/to/some/script.php
2
  • I have already set executable. If executed this command in the terminal it works the script.php: /usr/bin/php /path/test.php. I think in the crontab -e, you have to insert the php path of the machine Apr 16, 2015 at 15:50
  • does the script have #!/usr/bin/php in the first line? If it does, you do not need to call /usr/bin/php Apr 16, 2015 at 15:55
1

If you wanted to run a script every 5 minutes you should add this entry.

*/5 * * * * /usr/bin/php /full/path/to/php/script.php

1
  • That is what I wrote: */5 * * * * /usr/bin/php /var/www/mydomain.com/public_html/test.php, but nothing happens ! Apr 16, 2015 at 15:38
1

Make sure you have PATH variable set correctly on crontab, so it can locate your file.

You can simple put following line in top of crontab

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/path/to/newfile.txt

Try, following test

* * * * * touch /tmp/hello

Do following to redirect result

*/5 * * * * /usr/bin/php /full/path/to/php/script.php > /tmp/out.txt

Make sure your script running on command line.

/usr/bin/php /full/path/to/php/script.php

Use the -f option to execute the script:

*/5 * * * * /usr/bin/php -f /full/path/to/php/script.php

Tail logs file to see its executing every 5 min

tail -f /var/log/cron
6
  • I modify the question, so you can have a look at my file. Apr 16, 2015 at 18:41
  • Does your script executing on shell /usr/bin/php /full/path/myscript.php ?
    – Satish
    Apr 16, 2015 at 19:09
  • yes, it works great that line, but not with crobtab :( Apr 16, 2015 at 19:11
  • Lets try hello world program and see how it goes <?php Print "Hello, World!\n"; ?> and put that in your crontab * * * * * /usr/bin/php /tmp/helloworld.php >> /tmp/helloworld.out
    – Satish
    Apr 16, 2015 at 19:16
  • also try this */5 * * * * php /full/path/myscript.php don't use /usr/bin/php and put #!/usr/bin/env php in your script, sounds odd but i have seen this issue before in perl
    – Satish
    Apr 16, 2015 at 19:21

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.