3

I've been trying to execute a PHP file via a cron job, but I can't seem to get it to work. I've set the permissions of the relevant file to 777. Here is the line I've currently got in the crontab file.

30 12 * * * root /usr/bin/php -F /home/pi/Scheduled Tasks/dateCheck.php

The crontab file I'm editing is in the /etc folder.

2

Open up a terminal window and type the following command to create a cron job

crontab -e

Skip the following if already have a editor selected:

You should be prompted to choose an editor. Press enter to select nano as the default editor.

Now if you skiped the above then start again here:

You will now be looking at a empty file if this is the first time trying to schedule a cron job. If you already have cron jobs scheduled then you will see a list of cron jobs scheduled. Each line should contain a cron job.

Note: You are only viewing the cron jobs that are running for your current user. Most likely that is ‘pi’. Check out the commands below to see how you can view the cron jobs for other users as well as schedule cron jobs for other users.

Lets add a new scheduled task. The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.

The following command would run a PHP script every day at 6 am:

0 6 * * * /home/pi/testscript.php

This most likely makes no sense right now. Lets see how a cron command is designed.

Basic structure of the Cron Command

The first asterisk represents the minutes, followed by the hours, day of the month, the month, and day of the week. You can leave a point with an asterisk and it will execute the command on all the values. For example if you would like something to run everyday then you would leave the day of the month, month, day of the week with asterisk and only edit the values for minute and hours.

It may seem very annoying to write out the cron command if the time schedule is very complicated or long. I recommend using a cron command generator which allows you to select the time, days of week, and months to run your cron job.

Once you put a new cron job in the crontab go ahead and save it using CTRL+X and then Y to save your new cron jobs.

View Scheduled Task:

To view your currently scheduled cron job tasks type the following command:

crontab -l

You may need to run this command as sudo or it will not return anything. This command should list a line for every cronjob that is currently running.

That’s the basics of setting up a cronjob on the Raspberry Pi using the terminal. As usual be sure to comment below if you have any questions.

  • Thank you for your detailed answer, but unfortunately it doesn't work for me. The cronjob is saved but nothing happens when the scheduled time passes. – Ross Nov 2 '17 at 14:07
  • what about for test.. use @reboot then reboot – User98764431 Nov 2 '17 at 14:23
  • What do you mean by test? – Ross Nov 2 '17 at 14:25
  • to see if it works at all – User98764431 Nov 2 '17 at 14:26
  • I've now solved it by using the following: 30 12 * * * php /home/pi/ScheduledTasks/dateCheck.php - I checked the error log and it didn't like 'Scheduled Tasks' being two seperate words also '-f' seemed to be causing a problem. So it's working now. – Ross Nov 2 '17 at 14:59
2

I have now solved it myself by using this Cron Job Task:

30 12 * * * php /home/pi/ScheduledTasks/dateCheck.php 

I checked the error log and it didn't like Scheduled Tasks being two seperate words in .../Scheduled Tasks/...

  • php "/home/pi/words and spaces/x.php" will work. – goldilocks Nov 2 '17 at 15:10
  • @goldilocks Do you mean quotes around the path will make it work? – Ross Nov 2 '17 at 15:15
  • 1
    Yes, otherwise there is no way to distinguish that from calling the command with two arguments instead of one. Cron tasks are executed by a shell and follow the normal interpolation rules for that. – goldilocks Nov 2 '17 at 15:23
1

I use the default browser Chromium to execute php cron job. I insert into a php php page an html refresh:

echo("<meta http-equiv='refresh' content='19'>");

I select these way because need multiple cron jobs. Every php page can have it's own refresh time. All the php pages are loaded into an html page with iframes. At the end you can configure to load automatically the html page at the boot if you need.

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.