![]() |
|
|
#1 (permalink) |
|
Senior Member
Join Date: May 2009
Location: ~/
Posts: 127
|
Brief overview of cron, crontab and anacron.
Cron is a background daemon which executes scripts (cronjobs) at predetermined times or intervals. Crontab is a configuration file that allows you to select and schedule when, or at what interval, these cronjobs will run at. Anacron is smart program that works in conjunction with cron, but taking into accout that the system may be shut down from time to time or on a regular basis. Cron assumes that the system will be running for every scheduled job and if it's not then that job will not execute, while anacron takes into account that the system may be off and it executes each cronjob as close to the time as possible when the system is started. As you can see from this Synaptic package manager image, cron and anacron are already installed, but if they weren't, a few simple clicks in synaptic or an apt-get statement would take care of it. ![]() The little person that appears to be sitting in the image is one of the many desktop mascots (a feature of my current Linux distro Mangaka-chu). Lets start with the cron daemon... First we will determine if cron is currently running in the background. I will run a process command for the user root and pipe that into grep, looking for the "cron" pattern. ![]() As you can see from this image "cron" is indeed running. If it wasn't I run the following command as root or (sudo) NOTE: this is on my current Mangaka-chu system (Debian/Ubuntu based) # /etc/init.d/cron start or $ sudo /etc/init.d/cron start Alternatively stop and restart also work using the same syntax. The cron daemon will execute cronjobs under the permission of the crontab creator, most likely, you. Depending on you system, you may or may not have a /etc/cron.deny or a /etc/cron.allow file(s). These file(s) control a user's ability to create/edit crontab files.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
|
|
|
|
|
|
#2 (permalink) |
|
Senior Member
Join Date: May 2009
Location: ~/
Posts: 127
|
Now lets go over the crontab file...
The overall default crontab file consists of a few directives, (shell, path, mail, home variables, etc...) and then the scheduling and command sections. The scheduling section looks a little intimidating at first, but it's very simple if you have a legend to go by. Code:
---------------------- minute (0 - 59) | | ------------------ hour (0 - 23) | | | | -------------- day of month (1 - 31) | | | | | | ---------- month (1 - 12) or jan, feb, mar, apr, may... | | | | | | | | ------ day of week (0 - 6) (Sunday=0 or 7) | | | | | (or sun, mon, tue, wed, thu, fri, sat) | | | | | | | | | | * * * * * <user> <command> Directives <schedule> <user> <command> is valid for the overall default crontab file /etc/crontab, but a user's personal crontab has a much simpilar format. No directives <schedule> <command> use a single space between parts of the schedule and the command. Here is the overall default crontab file on my system: ![]() Now for some scheduling examples: * 02 * * * means every day at 2 O'Clock * */02 * * * means every 2 hours 01 * * * * (The first minute of every hour) */05 * * * * (every 5 minutes) 20 05 * * * (at 5:20 AM) * * 29 * * (29th of every month) * * * * 1-5 (Monday thru Friday) * * */02 * * (every 2 days) 01 22 12 1 * (January 12th @ 10:01 PM) In the command section you will enter a command or script to be executed. For example, let's use the "date" command, the date/time output could be appended to a local file like this: date >> /home/debtboy/log1.txt To create a personal crontab file... Open a shell and type "crontab -e" then enter you cronjob. As you can see from the image, I set a job (appending the date output to log1.txt file) to run on a 2 minute interval so every 2 minutes a newline get added to the file. ![]() After saving, everything is already working, no need to restart anything. Now I've decided to add a second cronjob to the file so I follow the same procedure, open a shell and type crontab -e and add the new cronjob. ![]() In this case a different file log2.txt will be appended on a 10 minute interval.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
|
|
|
|
|
|
#3 (permalink) |
|
Senior Member
Join Date: May 2009
Location: ~/
Posts: 127
|
I've been writing, and some time has passed, so lets see if our cronjobs are working...
![]() The 2 log files have been created, so far so good... now lets look inside. ![]() ![]() As expected, you can see the date/time appends to the log files log1.txt appends are each 2 minuets apart and the log2.txt appends are 10 minutes apart. As it stands now, these files will continue to grow until disk space runs out, so I better modify those jobs. One additional note: You don't need to know where your personal crontab file is located or the actual file name, but if your curious... It's located here: /var/spool/cron/crontabs/ The file name is the user name, in my case, it's called debtboy. Your system may be slightly different.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
|
|
|
|
|
|
#4 (permalink) |
|
Senior Member
Join Date: May 2009
Location: ~/
Posts: 127
|
Anacron needs very little explanation, you have 4 folders:
cron.hourly cron.daily cron.weekly cron.monthly ![]() As the root user or sudo, drop your executable script into the desired folder and that job will be executed as close to the interval as possible when the computer is running. To make your script(s) executable, add a she-bang #!/bin/bash as the first line in your script and then enter your commands. The executable bit(s) should be set after the file is saved via the chmod command. For example... script1.sh Code:
#!/bin/bash date >> /home/debtboy/log1.txt now enter chmod 755 ./script1.sh 755 = (rwxr_xr_x) now drop it in the folder of choice. These were a few cron/crontab/anacron basics via the command line, but there are graphical interfaces for those who prefer them as well as tasking programs on your favourite desktop environments. Give cron a try if your not using it already. That's all there is to it!! No need to schedule as the folder names speak for themselves.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
|
|
|
|
|
|
#5 (permalink) |
|
Maestro di Search
Join Date: Jul 2008
Posts: 4,295
|
Any idea if this can be a review article added to the Linux section at the main site?
__________________
Keep It Short and Sweet |
|
|
|
|
|
#7 (permalink) | ||
|
Senior Member
Join Date: Feb 2009
Location: 3rd largest island, smallest country there.
Posts: 230
|
Nice overview you got there, now for a short interview
.Quote:
What if I want something to run at a specific time in anacron (like 2.45PM everyday) or do I have to use cron/crontab to do that? Quote:
See here: http://www.ubuntugeek.com/amor-a-cre...r-desktop.html (Note that the article is incorrect, AMOR doesn't stand for Automatic Machine Object Recognition, just read the comments.) |
||
|
|
|
|
|
#8 (permalink) | |||
|
Senior Member
Join Date: May 2009
Location: ~/
Posts: 127
|
Quote:
With the exception of Ruby, I use Python, Tcl/Tk and Perl quite a bit, but most of my cronjobs are simple bash scripts. Quote:
Quote:
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
Last edited by debtboy; 09. Dec 2009 at 09:20 AM. |
|||
|
|
|
|
|
#9 (permalink) | ||
|
Senior Member
Join Date: Feb 2009
Location: 3rd largest island, smallest country there.
Posts: 230
|
Quote:
Python seems to be the easier to learn of the two languages so I'll stick with it for a while.Quote:
!Good luck with that. |
||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|