Can I modify cron job run time using php?

I have a web cam on my home network, facing a view which has spectacular sunsets and sunrises. I want to grab a picture, at, say 20 minutes before sunset and 15 mins after after sunrise each day. I can use php/cURL to grab a picture from the camera, give it a name/date/timestamp and save it.

I can use php to get the sunset/rise times for my lat/long into variables, and can write a .txt file in the ‘right’ cron job format that will overwrite the previous day’s file/times with the new ones, but can’t work out how (and where) to save the .txt file so that it is recognised as a cron job and runs.

Every ‘help’ page I read says something like ‘compose your cron file using crontab and your favourite editor’. I know that using crontab checks the syntax on saving, but assuming I have the right syntax can I just write my revised file to a specific location and avoid using the crontab/editor route? I’m using ubuntu linux BTW, and as far as I know everything in linux is treated/saved as a file, so that pesky crontab file should live somewhere… no?

Or, as a different approach, could I possibly run a static cronjob early each day, to run a php script that uses exec() to run the command ‘at’ with the new times in the at command. Any advice as to which way to go, (or another approach) would be welcome.




Have you looked at phpcron It does not use the Systems Cron but provides cron ‘like’ functionality. As it it opensource if out of the box it does not do what you need then you can hook into it with your own code.


How about running a cron job every 5 minutes, which calls a PHP script and checks to see if it should make a new photo or not?

Steve, I followed your link, to sourceforge and the home page link there is is 404 not found, with this message…"[I]Sorry, the page you were looking for:

is no longer available. [/I]

Immerse, Thanks for the suggestion, I’m going to run a ‘once a day’ cron job in the early morning, it will run my php script to get the day’s sunrise/set times and write them into a static text file. I will then do as you suggest and have a second job running every 5 minutes which will, each time it runs ‘include’ the first file with the day’s times, compare with the current time and take a picture if appropriate (ie times fall within the time window.)

This means I will be running the script, and including the day’s times file 720 times a day which does seem rather silly compared to just writing a new cron job once each day, so I’m still looking for an answer to my original question though! I appreciate that the cron daemon runs once every minute that the computer is on, so perhaps the overhead involved is minimal.

Why not just calculate the sunrise and sunset on every run, and check it? You seem to be overcomplicating things by writing a text file

You are probably right… I had already written the php to get sunrise/set times for my long/lat and then add/subtract my time windows, so had the 4 variables ready as timestamps. I’ll just copy/paste the code into the code run by the 5 min cronjob. :slight_smile: