You need to publish or change files overnight while you're sleeping.
Use your web server's built-in task scheduling utility, cron, to do the work for you.
Web designers like their sleep just like anyone elsemaybe more. The last thing any of us want to do is stay up late or get up early to post new information on a web site according to the boss or marketing department's schedule.
Fortunately, Unix-based web servers come with a built-in task-scheduling utility called cron that can do everything from executing simple file operationslike copying an updated web page from a private directory to a public oneto running complete scripts with instructions for more complex site maintenance routines. Let's look at the basics of cron and how you can use it to do simple site updates when you're otherwise busy having a life.
Say, for example, that your company's public relations department is working on an important news release for a new product announcement. The release is embargoed (held back from public view) until Wednesday morning, but they give you the final text of the release on Monday to build a page for the web site. The PR department wants the release to be posted on the site at 6:00 a.m. Eastern Standard Time, but your office is in Denvertwo hours behind the East Coastand you plan to be watching the back of your eyelids at that time. It's cron to the rescue!
First, create the updated web page (newsrelease.html) and upload it to your web server. If you're feeling lucky, and don't think that URL-fishing site visitors or Google will find the page before the embargo date, put the new page in a new public directory on your web site. If you're worried about your job security, you're paranoid, or both, put the file in a directory that is password protected, hidden, or outside the root web site directory on the server. Either way, cron will be able to find and move the file when the time comes.
Now, tell your web server to use cron to move the file to the URL that the PR department will announce on Wednesday morning. Your web server stores the list of tasks, or cron jobs, that it will run for your account in a file call a crontab. From the command-line prompt to your server, type crontab -l to list the tasks. Assuming there are no tasks yet, the server will respond with something like "no crontab tab for user doug." Type crontab -e to create a new crontab file using a command-line text editor.
crontab entries start with the time and day on which they should run, followed by the command. They are listed in this order:
Using the Unix move utility, mv, the crontab line for your scheduled site update should look like this, assuming the server is in the same time zone as you are:
0 4 * * 3 /bin/mv /private/newsrelease.html /public/newsrelease.html
Alternately, if the embargo date is the 15th of the month, you can use this line in your crontab:
0 4 15 * * /bin/mv /private/newsrelease.html /public/newsrelease.html
Asterisks are wildcards that cron will use to run the task on any day of the month, month of the year, and so forth. A crontab entry that begins with five consecutive asterisks will run on every minute of every day of the year.
Tweaking your crontab
To schedule a recurring task, say every 15 minutes, use this syntax:
*/15 * * * * /bin/mv /private/newsrelease.html /public/newsrelease.html
If your web server is located in a different time zone than you are, you can tell cron to use your local time for running automated tasks. Add a time zone configuration line to the top of your crontab like this:
Note that you can't easily change the overall time zone setting for your web serverespecially in a shared hosting setupbecause Apache takes its time setting from the server's system clock. If getting the correct time zone for things like time and date stamps on files and order receipts is important, consider hosting your account on a server that resides in your time zone. Or upgrade your hosting account to a virtual or dedicated server, which may give you more control over the server's clock, even if the server itself is in a different time zone.
Every time cron runs an automated task from your crontab, it will send an email to your login account's default inbox detailing the results of the command. Feedback from cron provides valuable debugging information, but emails from frequently recurring tasks can choke your inbox and eat up the disk quota on your hosting account. To turn off the notifications, add this line to top of your crontab:
Be sure to make the file you upload executable by the owner (you) by using the chmod utility to change the permissions on the file with this command:
chmod u+x /path/to/my_crontab_file
Then use this command to load the entries in your file into the server's notion of your crontab:
Then double-check that the crontab was loaded correctly from your file by typing:
This should output your crontab file exactly as you entered it:
*/15 * * * * /bin/mv /path/to/your/privateorhidden/directory/newsrelease.html /path/to/your/public/directory/newsrelease.html