Here is another excerpt from Easy Linux Commands. Though the book is geared toward Linux, most of the information (including this section on cron) applies to UNIX operating systems as well.

The cron Daemon

The cron daemon is the system task that runs scripted jobs on a pre-determined schedule. The crontab command is used to tell the cron daemon what jobs the user wants to run and when to run those jobs.

Each Linux user can have their own crontab file, if allowed by the System Administrator. The administrator controls use of crontab by including users in the cron.deny file to disallow use of crontab.

crontab Options

The crontab command has several options.

Option Purpose
-e edit the current crontab file using the text editor specified by the EDITOR environment variable or the VISUAL environment variable
-l list the current crontab file
-r remove the current crontab file
-u specifies the user’s crontab to be manipulated. This is usually used by root to manipulate the crontab of other users or can be used by you to correctly identify the crontab to be manipulated if you have used the su command to assume another identity.

Options for the crontab command

crontab can also accept a file name and will use the specified file to create the crontab file. Many users prefer to use this option rather than the crontab -e command because it provides a master file from which the crontab is built, thus providing a backup to the crontab. The following example specifies a file called mycron.tab to be used as the input for crontab.

$ crontab mycron.tab

Here’s how you would use the crontab –l command to view the current cron entries for the logged in user.

$ crontab -l

#**********************************************************
# Run the Weekly file cleanup task at 6:00AM every Monday
# and send any output to a file called cleanup.lst in the
# /tmp directory
#**********************************************************
00 06 * * 1 /home/terry/cleanup.ksh > /tmp/cleanup.lst

#**********************************************************
# Run the Weekly Management Report every Monday at 7:00 AM
# and save a copy of the report in my /home directory
#**********************************************************
00 07 * * 1 /home/terry/weekly_mgmt_rpt.ksh wprd > /home/terry/weekly_mgmt_rpt.lst

Now if we wanted to delete all the entries in the crontab we can use the –r option.

$ crontab -r

The Format of the crontab File

The crontab file consists of a series of entries specifying what shell scripts to run and when to run it. It is also possible to document crontab entries with comments. Lines which have a pound sign (#) as the first non-blank character are considered comments. Blank lines are completely ignored. Comments cannot be specified on the same line as cron command lines. Comments must be kept on their own lines within the crontab.

There are two types of command lines that can be specified in the crontab: environment variable settings and cron commands. The following sections will provide more detail on these two types of crontab entries.

Environment variable settings

Each environment variable line consists of a variable name, an equal sign (=), and a value. Values that contain spaces need to be enclosed within quotes. The following are some examples of environment variable settings:

color = red
title = ‘My Life in a Nutshell’

It is important to remember that variable names are case sensitive and that system variables are usually defined with upper case names, while user defined variables are defined with lower case names.

Note: Environmental variables are supported in the crontab for many Linux distributions but do not work on all platforms.

crontab Command Lines

Each crontab command line is comprised of six positional fields specifying the time, date and shell script or command to be run. The format of the crontab command line is described in Table 10.2 below:

Field Minute Hour Day of Month Month Day of Week Command
Valid values 0-59 0-23 1-31 1-12 0-7 Command path/command

crontab fields and valid values

NOTE: The use of 7 to indicate Sunday is not supported on all platforms. For best compatibility use 0 for Sunday.

Each of these fields can contain a single number, a range of numbers indicated with a hyphen (such as 2-4), a list of specific values separated by commas (like 2,3,4) or a combination of these designations separated by commas (such as 1,3-5). Any of these fields may also contain an asterisk (*) indicating every possible value of this field. This can all get rather confusing so let’s take a look at a few examples.

The next several examples are all part of the same crontab file. We have broken it up in order to explain each entry individually.

#**********************************************************
# Run the Weekly file cleanup task at 6:00AM every Monday
# and send any output to a file called cleanup.lst in the
# /tmp directory
#**********************************************************
00 06 * * 1 /home/terry/cleanup.ksh > /tmp/cleanup.lst

This entry will run the script cleanup.ksh at 0 minutes past the hour, 6 am, every day of the month, every month of the year, but only on Mondays. This illustrates that for a crontab to execute all of the conditions specified must be met, so even though we’ve said every day of the month by making the third field a wildcard, the day also has to meet the final condition that the day is a Monday.

#**********************************************************
# Run the Weekly Management Report every Monday at 7:00 AM
# and save a copy of the report in my /home directory
#**********************************************************
00 07 * * 1 /home/terry/weekly_mgmt_rpt.ksh wprd > /home/terry/weekly_mgmt_rpt.lst

This entry is very similar but will execute at 7:00am. Since the hour is in 24 hour format (midnight is actually represented as 00) we know the 07 represents 7:00 a.m. This entry again will only be run once a week.

#**********************************************************
# Weekly Full Backup - run every Sunday at 1:30AM
#**********************************************************
30 01 * * 0 /home/terry/full_backup.ksh wprd > /tmp/full_backup.lst

Here we have specified this script to be run at 30 minutes past the hour, the first hour of the day, but only on Sundays. Remember that in the day of the week column Sunday can be represented by either 0 or 7.

#**********************************************************
# Nightly Incremental Backup - run Monday-Saturday at 1:30AM
#**********************************************************
30 01 * * 1-6 /home/terry/incr_backup.ksh > /tmp/incr_backup.lst

In this crontab entry we see the same indication for hour and minute as the last entry but we have specified a range for the day of the week. The range 1-6 will cause the incr_backup.ksh to be executed at 1:30 every morning from Monday through Saturday.

#**********************************************************
# Low disk space alert ... run every 15 minutes, sending
# alerts to key individuals via e-mail
#**********************************************************
00,15,30,45 * * * * /home/terry/free_space.ksh > /tmp/free_space.lst

This entry has minutes separated by a comma indicating that it should be run at each of the indicated times. Since all the other fields are wildcards (*) the entry will be run on the hour (00), 15 minutes past the hour, 30 minutes past the hour and 45 minutes past the hour.

#**********************************************************
# Lunch Time Notification - run Monday-Friday at Noon -
# sends a message to all users indicating it's lunch time
#**********************************************************
00 12 * * 1-5 /home/terry/lunch_time.ksh wprd > /tmp/lunch_time.lst

This lunch reminder is set up to run at 12:00 p.m. Monday through Friday only.

The most important thing to remember is that a crontab entry will execute every time all of its conditions are met. To take the last entry as an example, any time it is 00 minutes past the hour of 12 on any day of the month and any month of the year and the day of the week is between Monday and Friday inclusive (1-5) this crontab will be executed.

You will use wildcards in most crontab entries but be careful where you use them. For instance, if we mistakenly placed a * in the minute position of the last crontab example above we would end up running the script for ever minute of the 12:00 hour instead of just once at the beginning of the hour. I don’t think anyone needs that many reminders to go to lunch, do you?

As mentioned above, the day-of-week field accepts either zero or seven as a value for Sunday. Any of the time/date fields can also contain an asterisk (*) indicating the entire range of values. Additionally, month and day-of-week fields can contain name values, consisting of the first three letters of the month, as indicated in the table below. These may not be supported on all platforms so are rarely used.

Field Valid Entries (case insensitive)
Days of the week sun, mon, tue, wed, thu, fri, sat
SUN, MON, TUE, WED, THU, FRI, SAT
Months of year jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC

Day of week and Month abbreviations.

Note: The use of these abbreviations is also not supported on all platforms. For best compatibility use numeric indicators for day and month.

When numbers are used, the user can specify a range of values separated by a hyphen or a list of values separated by commas. In other words, specifying 2-5 in the hour field means 2AM, 3AM, 4AM and 5AM, while specifying 2,5 means only 2AM and 5AM.

We’ve talked an awful lot about how to specify the date and time in the crontab but what about the command? Well, most folks will write shell scripts to execute with their crontab entries but you can actually just execute a command from the crontab as well. Either way make sure you put the absolute path to your command in the crontab.

If the command or script you call in your crontab typically sends output to the screen you will probably want to redirect that output to a log file with the >> symbol so you can check it later. Be careful with this as the log files may get rather large over time!

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


unix, linux, cron, crontab, system administration, sysadmin, job scheduling

There are some files within the home directory that are ordinarily hidden. Hidden files have names that begin with a period; hence, they have been given the nickname of dot files. Hidden files are not displayed by the ls command unless the –a option is used in the format of ls –a.

The table below lists some of the more common dot files that users should know about. This is by no means a totally comprehensive list. Additional dot files can be found in the user’s home directory; however, some searches may not find some of the files listed here. The files found are dependent upon the applications installed on the server, the utilities that are in use and the command shell that is being used. Since the default shell for Linux is the bash shell, the home directory typically contains the bash related scripts indicated below.

File

Description

.bash_history

For users of the bash shell, a file containing up to 500 of the most recent commands available for recall using the up and
down arrow keys.

.bash_logout

Script that is run by the bash shell when the user logs out of the system

.bash_profile

Initialization script that is run by the bash shell upon login in order to setup variables and aliases. When bash
is started as the default login shell, it looks for the .bash_profile file in the user’s home directory; if not found, it looks for .bash_login.
If there is no .bash_login file, it then looks for a .profile file.

.bashrc

Initialization script executed whenever the bash shell is started in some way other than a login shell. It is better to put
system-wide functions and aliases in /etc/bashrc, which will be presented later in the book.

.gtkrc

GTK initialization file. GTK+
is a multi-platform toolkit for creating graphical user interfaces, used by a
large number of applications. It is the toolkit used by the GNU
project’s GNOME desktop.

.login

The initialization script that is run whenever a user
login occurs.

.logout

The script that is automatically run whenever a user
logout occurs.

.profile

Put default system-wide environment variables in /etc/profile.

.viminfo

Initialization file for the Vim text editor that is
compatible with vi.

.wm_style

Specifys the default window manager if one is not
specified in startx

.Xdefaults & .Xresources

Initialization files for Xterm resources for the user.
Application program behavior can be changed by modifying these files.

.xinitrc

The initialization file used when running startx, which can be used to activate applications and run a particular window manager.

.xsession

This file is executed when a user logs in to an X-terminal
and is used to automatically load the window manager and applications.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


unix, linux, system administration, sysadmin, hidden files, config files

Here’s a quick description of the scp command used for securely copying files between systems on Linux and UNIX.

The scp (secure copy) command can be used to copy files or even entire directories to a remote host. scp is a replacement for the rcp (remote copy) command. While rcp provides the same functionality it is not encrypted and therefore not secure. The scp command uses SSH for data transfer, providing SSH level security.

scp takes arguments in the form of scp –options source destination. The most common option is the –r (recursive) option which is necessary if you are using scp on multiple files or on directories. The source and destination arguments can specify not only a path but optionally a username and hostname in the format of username@hostname:path. If the username is omitted scp assumes the username of the current logged in user. If the hostname is omitted scp assumes the path provided is on the current system. Typically either the source or the destination will describe a remote system; however you could use scp to move a file or files from one remote system to another.

The following example copies the file secret.dat from user tclark’s home directory to a directory named /backup/tclark on a server named backup_server logging in as user terry. As indicated below, the scp command will prompt for the password for user terry on the backup_server when the connection is attempted.

$ scp /home/tclark/secret.dat terry@backup_server:/backup/tclark
terry@backup_server's password: password
secret.dat 100% 1011 27.6KB/s 00:00

Zach has a good article about setting up ssh with key authentication which will allow you to use the scp and ssh commands without a password while still maintaining security.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


linux, unix, system administration, sysadmin, easy linux, scp

Often we only need to see the beginning or end of a file to find what we’re looking for. The head and tail commands offer exactly this functionality. Here’s some more info on these commands from Easy Linux Commands.

Displaying Beginning Lines of a File

Sometimes a user might have a large file for which they only need to display the first few lines. For instance, perhaps the user would like to see the error code on a dump file and the code and error messages appear within the first fifteen lines of the dump file. The following example demonstrates how to display the first fifteen lines of a file using the head command. The head command takes a number as an option and uses it as the number of lines to be displayed. The default is 10.

$ head -15 declaration.txt
The Declaration of Independence of the Thirteen Colonies
In CONGRESS, July 4, 1776

The unanimous Declaration of the thirteen united States of America,

When in the Course of human events, it becomes necessary for one people to dissolve the political
bands which have connected them with another, and to assume among the powers of the earth, the
separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent
respect to the opinions of mankind requires that they should declare the causes which impel them
to the separation.

We hold these truths to be self-evident, that all men are created equal, that they are endowed by
their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit
of Happiness. --That to secure these rights, Governments are instituted among Men, deriving their
just powers from the consent of the governed, --That whenever any Form of Government becomes

In this example and often in use it may seem like head is displaying more lines than you asked for. That typically is because the lines are too long for the display so a single line may be continued on the next line.

Displaying Ending Lines of a File

The need might arise to see only the last lines of a file. A good example of this might be an error log file where the user would like to see the last few messages written to the log. The tail command can be used to display the last lines of a file, while passing the number of lines to be displayed. The following example requests the last eight lines in the file called declaration.txt.

$ tail -8 declaration.txt
they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the
same Object evinces a design to reduce them under absolute Despotism, it is their right, it is
their duty, to throw off such Government, and to provide new Guards for their future security.
—Such has been the patient sufferance of these Colonies; and such is now the necessity which
constrains them to alter their former Systems of Government. The history of the present King of
Great Britain [George III] is a history of repeated injuries and usurpations, all having in
direct object the establishment of an absolute Tyranny over these States. To prove this, let
Facts be submitted to a candid world.

Again it appears we are getting more than eight lines, but this is just the result of long lines wrapping onto two lines.

Display Active Writes to a File

Sometimes you need to go one step further and watch as lines are being written to a file. Perhaps, for example, an application is compressing and copying files to an alternate location, writing messages to a log file called message.log as it processes each file. A curious user might want to observe the progress of the application. In this case, the tail command with the –f (follow) option can be used to read the messages as they are written to a file. The following example assumes that the current working directory is the same directory where the log file resides.

$ tail -f message.log

A clever Linux user can also use the less command to display the beginning lines of a file, the ending lines of a file, or to follow active writes to a file like tail –f does. See the man entry for the less command to see how this is done.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


My book Easy Linux Commands: Working Examples of Linux Command Syntax is now on shelves, as seen in this picture from Borders Book and Music right down the street from my home in Concord, NH.

Easy Linux Commands at Borders Concord


Written by me and Terry Clark this book is part reference and part tutorial encompasing everything from basic file manipulation to administrative commands.

From an Amazon review by Kurt:

New users to Linux and old-school Unix engineers alike will find value in this book. The author did a tremendous service representing this rapidly growing technology in an easy to read, easy to follow humorous format. Page to page there are examples of basic Unix commands and obscure Linux features available to most builds. Having built my career in deploying “Enterprise Class” Unix based solutions for today’s high availability needs, it is refreshing to learn some new tricks and be reacquainted with old tools built discussed in this book. I recommend it to entry level and veterans alike.

The book is available in major bookstores (although any bookstore should be able to order it for you) and can also be ordered from the publisher for just $19.95. That’s 30% off the cover price!

Thanks to Zach for snapping the pic and sending it on to me.

linux, unix, linux commands, command line, redhat, ubuntu, book, technology, information technology, system administration, sysadmin

« Previous PageNext Page »