Wednesday, November 16, 2011
3:00 PM

How to Automate Tasks in Linux

Whether you are running a Linux server or simply using it on your desktop computer, some repetitive tasks can be a pain if you have to do them every day. It may be as simple as running a backup program at the end of the day, but with so many things going on, you probably do not want to take the chance that you might forget to do it.
Fortunately for the hard working Linux system administrator, a program called cron is here to rescue you from repetitive tasks. With cron you can automate just about anything, from programs to custom-made scripts. Cron is a daemon, meaning it runs in the background and is always on once you start your computer or server. It reads a file called “crontab”, which tells it when to run the programs or scripts listed in it.

Crontab

Crontab is typically located at /etc/crontab and is a single text file containing information that cron uses to determine when it should execute a command string. A crontab file may have the following fields:
m - The minute of the hour
h - The hour of the day (on a 24-hour clock)
dom - The day of the month
mon - The month of the year
dow - The day of the week
user - The user who will execute the command string
command - The command itself
For example, the default Ubuntu crontab looks like this:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow usercommand
17 ** * *root cd / && run-parts --report /etc/cron.hourly
25 6* * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6* * 7roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 61 * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
The third command will run weekly on the seventh day of the week at 6:47 AM. If you wanted to run a command at 8:30PM every day, the string would look like this:
30 20 * * * username /usr/bin/command
Cron.daily, weekly, monthly, and hourly
In addition to the standard crontab, many Linux distributions have a set of directories located in /etc that make it easy to setup cron jobs. These are /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly, and /etc/cron.hourly. To use them, all you have to do is drop your script into one of these directories. Cron will start running the script as soon as the next rotation begins.
If you want to run a script or command from another location, you can create a symbolic link to it within one of the cron directories.
ln -s /usr/local/bin/mybackupscript.sh /etc/cron.daily/mybackupscript.sh

Graphical Frontends

If you are using Linux on a server, manually editing crontab from the command line may be the best and most efficient option, but many web-based control panels also include graphical interfaces for editing crontab or even putting scripts into cron.daily and the other cron directories.
Not all shared hosting accounts will allow you to run cron, but many web hosts, such as virtual servers company 34SP.com, now include cron functionality from within their control panels even for shared hosting users.
For desktop/laptop Linux users, you can use a GUI program to create automated tasks. Two options are Gnome-schedule, which is available in many Linux distro repositories, and KDE’s Task Scheduler (formerly called Kcron), which is a standard part of most KDE installations. Both of these programs use human-readable strings such as “Every hour” rather than the standard cron language of numbers and *’s.
As you can see, cron is a powerful and easy method for automating tasks in Linux on both the server and desktop. It is a standard part of Linux, and is most likely running on your Linux computer or web server right now even if you were unaware.

Tavis J. Hampton is a Linux system administrator and writer for TavisOnline.com.

0 comments:

Post a Comment