I have Raspberry Pi with Raspbian installed on it. I need a cron job that needs to have a variable time period (every 10, 20, 30, 40, 50 or 60 minutes). I found out the easiest way to create new cron job without editing crontab file is to create new file in /etc/cron.d. My file /etc/cron.d/myCron looks like this:

*/10 * * * * root /home/pi/myScript >> /home/pi/myLog.log 2>&1

and its owner/permissions:

-rw-r--r-- 1 root root  84 Sep  9 16:15 myCron

Now I need to write to this file via php script, something like this:

$config = $_GET["config"];
$cron = fopen('/etc/cron.d/myCron','w');
fwrite($cron,"*/".$config['interval']." * * * * root /home/pi/myScript >> /home/pi/myLog.log 2>&1")

in order to change the time period.

Problem is, php is run by user www-data and the files in cron.d directory needs to be owned by root and has to have permissions -rw-r--r--, otherwise it will not work.

Any ideas on how can I achieve this? Thanks!

1 Answer 1


One method, use crontab to install your new file, see the crontab - option.

Obviously you'll then encounter problems updating root's crontabs, but sudo can allow you to do that.

/var/spool/cron/crontabs will be used for the final destination location instead /etc/cron.d.

For best security (not sure if this is important in your situation), try to do as much as possible with the least permission, then step up as root for the final run of your script.

It is possible to configure sudo to allow www-data to to run only one particular shell script as root, which performs the actual update. Then, the bare minimum is permissioned. ie. creating your file can be done as www-data.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .