Gizmos Freeware Reviews  

Go Back   Gizmo's Freeware Forum > Freeware Forum > Linux

Reply
 
Thread Tools Display Modes
Old 09. Dec 2009, 03:37 AM   #1 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default The Cron Basics:

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]
debtboy is offline   Reply With Quote
Old 09. Dec 2009, 04:04 AM   #2 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

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>
This format:
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]
debtboy is offline   Reply With Quote
Old 09. Dec 2009, 04:17 AM   #3 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

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]
debtboy is offline   Reply With Quote
Old 09. Dec 2009, 04:40 AM   #4 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

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
save it as script1.sh
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]
debtboy is offline   Reply With Quote
Old 09. Dec 2009, 04:56 AM   #5 (permalink)
Maestro di Search
 
Jojo Yee's Avatar
 
Join Date: Jul 2008
Posts: 7,810
Default

Any idea if this can be a review article added to the Linux section at the main site?
Jojo Yee is offline   Reply With Quote
Old 09. Dec 2009, 06:13 AM   #6 (permalink)
Senior Member
 
wdhpr's Avatar
 
Join Date: Sep 2008
Location: The north Coast
Posts: 1,513
Default

Very nice

Scheduled task's for linux.
I use Windows about 70% of the time so decided to use anacron.

Cheers
Wdhpr
wdhpr is offline   Reply With Quote
Old 09. Dec 2009, 08:29 AM   #7 (permalink)
Senior Member
 
Join Date: Feb 2009
Location: 3rd largest island, smallest country there.
Posts: 229
Default

Nice overview you got there, now for a short interview.

Quote:
Originally Posted by debtboy View Post
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
Can I use Python or any other scripting language instead of Bash?

Quote:
Originally Posted by debtboy View Post
No need to schedule as the folder names speak for themselves.
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:
Originally Posted by debtboy View Post
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).
Does the little person do anything? I have something similar to that little person in my desktop, AMOR (Amusing Misuse Of Resources), it's a creature that does random stuff in your desktop (depending on the character, it can fly around with a jet pack, teleport, fall down from the top of any open application that you just closed or minimized, show tips and etc).

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.)
bk_7312 is offline   Reply With Quote
Old 09. Dec 2009, 10:07 AM   #8 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

Quote:
Originally Posted by bk_7312 View Post
Can I use Python or any other scripting language instead of Bash?
Of course you can use Python, Tcl, Perl, Ruby, C, etc...
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:
Originally Posted by bk_7312 View Post
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?
Yes, if you want that kind of control, you will have to use a crontab file. An additional advantage of the crontab, is you don't have to be root to use it, where anacron requires root privileges.


Quote:
Originally Posted by bk_7312 View Post
Does the little person do anything? I have something similar to that little person in my desktop, AMOR (Amusing Misuse Of Resources), it's a creature that does random stuff in your desktop (depending on the character, it can fly around with a jet pack, teleport, fall down from the top of any open application that you just closed or minimized, show tips and etc).
Yes, the little person does small (in place) animations, replys when clicked on, etc..., but not to the extent of your characters. I'm currently looking into how that system works to make a few hacks.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]

Last edited by debtboy; 09. Dec 2009 at 10:20 AM.
debtboy is offline   Reply With Quote
Old 09. Dec 2009, 12:00 PM   #9 (permalink)
Senior Member
 
Join Date: Feb 2009
Location: 3rd largest island, smallest country there.
Posts: 229
Default

Quote:
Originally Posted by debtboy View Post
Of course you can use Python, Tcl, Perl, Ruby, C, etc...
With the exception of Ruby, I use Python, Tcl/Tk and Perl quite a bit, but most of my cronjobs are simple bash scripts.
Good to know, now I don't have to feel bad about learning Python instead of bash for the time being. Python seems to be the easier to learn of the two languages so I'll stick with it for a while.

Quote:
Originally Posted by debtboy View Post
Yes, if you want that kind of control, you will have to use a crontab file. An additional advantage of the crontab, is you don't have to be root to use it, where anacron requires root privileges.
Well, you can't win them all!

Quote:
Originally Posted by debtboy View Post
Yes, the little person does small (in place) animations, replys when clicked on, etc..., but not to the extent of your characters. I'm currently looking into how that system works to make a few hacks.
Good luck with that.
bk_7312 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT +1. The time now is 10:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.