Tag Archives: Pi PHP and CRON


Boy am I in it over my head (for now). my new Raspberry Pi turned up – very nice – formatted a 32Gig microSD, loaded Raspbian and after a couple of false starts I soon had a working (graphical worktop) operating system.  I added FTP, VNC (so I don’t have to tie up a monitor, keyboard and mouse) and PHP,  I managed to get my PHP MQTT page to run and send messages to the MQTT Broker.

So this works.

No problem.

I THOUGHT I was having trouble getting a CRON job to work (timed running of the page) – but it turns out it’s worse than that..   I’ve been trying “PHP” then the location of the page… and I’ve realise that won’t work – so in a terminal..

php  /var/www/mqtt/examples/publish.php  – the page runs but relative paths in the page are wrong… surely there is a way to run a page like that as if it was running in a browser (clearly without any client side stuff).

So in the CRON page I had

* * * * * php /var/www/mqtt/examples/publish.php

and NOTHING was happening…  and of course now I know at least one reason. I’m assuming the other reason will be something to do with permissions…

So can anyone familiar with the PI point me in the right direction.. how do I get a CRON job to run the page with internal relative links working as if it was being run from a browser.

And if we get that far, am I going to hit permissions issues. I used sudo crontab –e to edit a simple text file with the above line in it and saved it in /etc/cron.d

All of which is probably wrong.

Incidentally don’t try installing THINGBOX on the Pi2 – it won’t work and they don’t have a way around it yet. In my case it would not even start up.

Solution: Although no-one had a single solution I got enough out of all you helpful people to resolve this and more.

So firstly, it’s not /usr/local/bin that PHP resides it’s /usr/bin and you can use any old editor to edit any old file name in /etc/cron.d to do the job.

Given my hatred of command lines I found the command line editor to be ATTROCIOUS but.. LeafEdit in the desktop is great (well ok, it’s not NOTEPAD++ is it) – it’s only failing is it’s not running as root. Now there might be an easier way but I went into the terminal and ran “sudo leafedit” and up popped the graphical editor with all the permissions I needed – I updated my file calld cron to include the path for php, saved it and then ran “crontab cron” – no idea what that does – but next thing I knew, my PHP messages were appearing in Mosquitto (being monitored by mqtt-spy on the PC) no problem.

I rebooted the PI – no problem – it all came up roses and no modifications to the original PHP file at all. That’s what I call A RESULT.