|
abbeyvet -> RE: Cron Jobs? (7/12/2004 11:52:02)
|
The easiset way to explain this is by using an example. First the Cron daemon. Basically it is a little program that runs all the time on Linux servers, in the background, unnoticed and mostly doing nothing at all. Every so often (usually every minute), it checks to see if there is anything it should do. If you want to do something on your site or on the server at a specific time every so-many days, you just need something that the daemon will see when it looks and obey. This is called a crontab. It's just a file that has information about the minute, hour, day of month or day of week a specific command should be run. When the daemon sees this, it obediently causes the command to run at the given time. In practical terms, this is very handy. For example, on a site I manage we send out a newsletter to several thousand people every week. Some messages bounce, sometimes because an address is temporarily unavailable, sometimes because it has disappeared forever. Dealing with this by hand is a big pain. So, we have a little program that handles the bounces, resending mail twice in case of errors and then suspending the addresses from the list if they bounce more than twice - on the assumption that at that point they are dead. This is the command that is in the crontab to make that all happen. 30 1 * * * /usr/bin/wget -O /dev/null -T 0 http://www.domain.com/mail/dailymail.php?pw=passowrd That looks horrible - but break it down and it is simpler. 30 1 * * * This bit says when the job should be done, in the order minute | hour | day of month | month | day of week So my command will run at 1.30 am every day, as I have * * * for day of month, month, and day of week so they are ignored. another example: 30 1 4 5 * that could be used to send a birthday greeting to someone at 1.30 am on the 4th of May each year [:D] The next bits are pretty grim looking: /usr/bin/wget This is the command bit - wget is a little tool that grabs a web page, so basically this is saying "go to wget, which is in the directory /usr/bin, and tell it this......" what it tells it is: -O /dev/null which if you are wget means "get the page but do not output it to a browser as you normally would, just run it". -T 0 This tells it not to time out - in other words to stick with the task it is being given! http://www.domain.com/mail/dailymail.php?pw=password This tells it the page to get, and the password it needs to get into it. Once that has been done the page dailymail.php will run on the server and the code in it will do whatever it has been written to do, which in this case is resend bounced messages. It's important to note here that Cron did nothing much - all the work was done by the file dailymail.php. All the cron task did was find the file and run it at a specific time. That is what Cron is almost always used for - running a specified program or script at a specified time. Some hosting setups allow you to enter your own cron tasks, but most do not. However your host will almost certainly be happy to set one up for you.
|
|
|
|