Getting scripts to execute autonomously at preset times

Say, for instance, I want PHP to send me an email every morning at 7:00 AM reminding me to wear socks. How would I go about making a script that would execute on its own at a specified time every day?

I’d say cron jobs or a meta refresh?

Though I have no idea what either of those are beyond the brief Googlings I just did, I’m still not sure if either of those are useful (but it may just be my ignorance).

Regarding what I believe to be meta refreshes, I suppose if the user opened their browser window to a certain page once a day I could get the current time and calculate the time until I wanted the page to refresh which could somehow send an email (cookies?). But as far as I can tell it would still require the user to open the page once a day and then leave the browser open all night long.

As for what I believe to be cron jobs, I don’t have root access to the server (it’s a school account) so I don’t know if I can even access those settings, and even if I could, Linux frightens me.

Somehow I figured that this was something that people did routinely. Was I wrong, or do they use something other than PHP?

No people, including me, with several scipts, do this all the time and as Zyppora said, Crons and Meta-Refreshes are the way to go.

For example I have a script that checks my email every Weekday, M-F, between the hours of 7:00AM to 6:00PM. If it finds any unread emails it counts them and then texts messages my phone as to how many unread messages there are. I use cron jobs for this. I could have however used Meta-Refresh simply by leaving a browser window open on my computer and have it refresh the same way, just would have had to add in some date functionality.

Maybe if you explained a little more about what the script should do, maybe we can help.

Just So You Know: Linux is pretty common place when it comes to PHP and you should get used to it if you are going to be working a lot with PHP.

Most Linux systems would allow you to set USER cron jobs too. I the command is crontab -e

You can then enter your information (I believe VI is the default editor too.)

The format of a crontab file (which is only plain text) is as follows

M  H  DOM  MON  DOW  Command

Where:
M = minutes -------------> 0-59
H = Hours ----------------> 0-23
DOM = Day of month —> 1-31
MON = Month ------------> 1-12
DOW = Day of week ----> 0-7 (0 or 7 is Sun)
Command = the command that you wish to run.

If you have more than one “Element” for a particular item, you separate them by a comma, otherwise the fields are separated by (delimited) a space.

Every field must have a value. Using an * (asterisk) means you want it to run EVERY time that “unit” arrives on the clock/calendar.

For example if I was having problems with my ISP and they wanted me to reboot my router every time I got disconnected. Well it was happening several times a day (even when at work), and no one was there to reboot it. So I wrote a script (called pingcheck) that would see if I could ping a web address (outside of my network), and if it could not, it would send a reboot command to the dsl. I wanted it to run every 30 minutes… So I did the following

00,30 * * * * /cron-stuff/pingcheck1 > /dev/null

The command is /cron-stuff/pingcheck1 but I also use the Redirect output to /dev/null because this is unattended and I don’t want to have to deal with prompts on the screen. You could just as easily redirect it to a file (to be used as a log maybe).

I eventually expanded it to run a different Pingcheck (so as to check MULTIPLE sites at different times. So I updated my cron jobs

00 * * * * /cron-stuff/pingcheck1 > /dev/null
15 * * * * /cron-stuff/pingcheck2 > /dev/null
30 * * * * /cron-stuff/pingcheck3 > /dev/null
45 * * * * /cron-stuff/pingcheck4 > /dev/null

Each of the above will run once every hour (at the specified minutes of 00,15,30 and 45)

Now that they have fixed my problem, If I wanted it to run just once a day at let’s say 6:43 AM I could modify it as follows:

43 6 * * * /cron-stuff/pingcheck1 > /dev/null

Or once a week (let’s go with Monday) at 6:43 AM

43 6 1 * * /cron-stuff/pingcheck1 > /dev/null

Or every Monday, Wednesday and Friday at 6:43 AM

43 6 1,3,5 * * /cron-stuff/pingcheck1 > /dev/null

Be careful about the Asterisk though… It means EVERY unit of time for it’s element.

for example

* * 1,3,5 * * /cron-stuff/pingcheck1 > /dev/null

The above would only run Every Monday, Wednesday and Friday, however, it would run 1440 times on each of those days (Every minute for every hour on Monday, Wednesday and Friday )

I hope this helps to clarify the Cron jobs. Also have a look at http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 for the manual page for a cron job.

Thanks a whole lot, that’s actually very helpful. I’ll definitely be using that in the future. :D

Glad I could help. I actually love using the cronttab.

Sponsor our Newsletter | Privacy Policy | Terms of Service