Web Tool Bag  
Home · Articles · Downloads · Discussion Forum · Web Links · News Categories · Synonyms DatabaseJuly 15 2024 00:02:55
Discussion Forum
Web Links
News Categories
Synonyms Database
Users Online
Guests Online: 1
No Members Online

Registered Members: 856
Unactivated Members: 118
Newest Member: lakim
Forum Threads
Newest Threads
Error: Cannot find m...
Uncaught Error: _reg...
Module build failed:...
mochi script questions
Hottest Threads
Installation [12]
Any questions and... [5]
Captcha picture d... [4]
Integrate with Vi... [4]
Mods: Sucess/Than... [4]
Latest Articles
Ubuntu: the vpn conn...
Howto Install HP Pri...
ReactJS progress met...
Accessing the Crontab

Accessing the Crontab

The following commands would apply to the user which you are logged in as. For example, if you are logged in as "root", these commands would pertain to root's crontab file.

crontab -e - opens the user's crontab file for viewing/editing

crontab -l - simply lists the crontab file's contents for the user. Think of it as a "cat" function for the crontab.

crontab -r - removes the crontab file contents for the user

But what if you want to edit another user's crontab?

The system administrator is usually logged in as "root", but making changes to another user's crontab file or simply looking at another user's crontab file is often necessary. For situations like this, you can append the "-u" flag followed by the desired username.

For example, if logged in as root but you want to edit the crontab for the user "admin", you would do the following:

crontab -e -u admin

The same logic applies to the other crontab commands as well, such as:

crontab -l -u admin - lists the crontab entry for the "admin" user.

crontab -r -u admin - removes the crontab entry for the "admin" user.

Writing to the Crontab

Now that you know how to access the crontab, let's take a look at the syntax of the crontab entry itself.

A typical crontab entry might look like this:

30 0,12 * * * /usr/local/scripts/whatever.script

OK, so what does that mean? Well, there are 2 parts to the entry you see above. In fact, any crontab entry has 2 parts:

 Part 1 - The schedule

The schedule, which governs when the task will run, consists of a string of numbers, possible commas and asterisks (*).

So, in the above example, the schedule is:

30 0,12 * * *

What you are seeing is actually split up into 5 sections. The following chart illustrates what each section of the schedule is for:

1. Minute - Minutes after the hour (0-59).
2. Hour - 24-hour format (0-23).
3. Day - Day of the month (1-31).
4. Month - Month of the year (1-12).
5. Weekday - Day of the week. (0-6, where 0 indicates Sunday).

* = An asterisk in a schedule field indicates "every". It means that the task will occur on "every" instance of the given field. So a "*" on the Month field indicates the the task will run "every" month of the year. A * in the Minutes field would indicate that the task would run "every" minute.

, = A comma is used to input multiple values for a field. For example, if you wanted a task to run at hours 12, 15 and 18, you would enter that as "12,15,18".

Let's take a look at how this format fits into the syntax of a crontab entry:

    _________________________ 1. Minute - Minutes after the hour (0-59)
   |      ______________________ 2. Hour - 24-hour format (0-23).
   |     |
   |     |      ___________________ 3. Day - Day of the month (1-31)
   |     |     |     
   |     |     |      ________________ 4. Month - Month of the year (1-12)
   |     |     |     |   
   |     |     |     |     ______________5. Weekday - Day of the week. (0-6, where 0 indicates Sunday)
   |     |     |     |    |
  30 0,12  *    *    * /some/script/or/command

So, when we combine all the schedule elements, we know when and how often this task will run. Going on the above example, this task would run:

At 30 minutes past the hours of 0 (midnight) and 12 (noon), EVERY day of the month, EVERY month of the year and EVERY day of the week.

In other words, the above task would run every single day at 12:30AM and 12:30PM.

Let's play around with the schedule a little bit and try something different. What if we had something like this:

15,45 0,12,6 20 1,2,3 0 /some/script/or/command

Wow! Now this is a pretty complex crontab entry. Let's decipher it...

15,45 - This means that the task will run at 15 and 45 minutes past the hour. But what hours and what days? Well, that's coming up.

0.12.6 - The task will run during the hours of 0 (midnight), 12 (noon) and 6AM.

20 - The task will run on the 20th day of the month. But during what months?

1,2,3 - The task will run only during the months of January, February and March.

0 - The task will only run on a Sunday.

So, when we put all of this information together, here is what it boils down to:

This task is going to run at 0:15, 0:45, 6:15, 6:45, 12:15 and 12:45 on the 20th of January, February and March IF that day falls on a Sunday. Pretty cool, huh?

 Part 2: The task itself

This is pretty self-explanatory. In the above example, the "task" to run would be: /some/script/or/command.

Now obviously, that's not a real script or commnad, it's just an example that you would fill in the blanks for. Generally, there are 2 ways to run a task from the crontab:

     Method A: Run the desired command directly from the crontab

Here's an example of running a command directly from the crontab:

*/15 * * * * /sbin/ifconfig | mail myname@mydomain.com

This is pretty cut and dry. This entry is going to run the "ifconfig" command and then pipe the results out to an e-mail that gets sent to myname@mydomain.com. This task will run EVERY 15 minutes, EVERY day, EVERY month, EVERY day of the week. In other words, this task runs every 15 minutes.

As you can see, the "ifconfig" command is called directly form the crontab entry. This is fine is you are running a simple command. But what if you need to do more than that?

     Method B: Run a script from the crontab that runs the commands.

Here's an example of running a script from the crontab. The script that you run, in turn, executes whatever instruction you might want:

15 0* * * /usr/local/scripts/run_webstats.script

As you can see, every night at 0:15, this task runs a script called "/usr/local/scripts/run_webstats.script". That script, as you may guess, generates the webstats for the website on the server. The crontab runs a script and then the script contains the actual instructions on what the task is supposed to do. Get it?

By the way, I'm just making these crontab scripts up out of thin air, so don't expect the above example to really work. You would need to replace my bogus script calls with REAL scripts calls or commands.

Posted by admin on January 17 2007 13:24:17 7823 Reads · Print
Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.


Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Member Poll
Which PHP framework do you preffer?





eZ Components






You must login to vote.
You must login to post a message.

03/10/2011 18:17
Hi, How to remove Register from Login screen? I don't want them to register and have full access! if you leave register then they should not have any rights until the admin assigns them

26/09/2011 08:28
Please describe your problem with more details. Thank you.

22/11/2010 18:31
Help. There was a problem with the request; error regarding feedbackzdr form program

Custom web software development by Devzone Tech
Copyright © 2024 - www.webtoolbag.com