Linux CH7 - Scripting
Linux CH7 - Scripting
Administration
CH7: Scripting
The first step is often the hardest, but don't let that stop you. If you've ever wanted to learn how to write a shell script
but didn't know where to start, this is your lucky day.
If this is your first time writing a script, don't worry — shell scripting is not that complicated. That is, you can do
some complicated things with shell scripts, but you can get there over time. If you know how to run commands at the
command line, you can learn to write simple scripts in just 10 minutes. All you need is a text editor and an idea of
what you want to do. Start small and use scripts to automate small tasks. Over time you can build on what you know
and wind up doing more and more with scripts.
Scripting
1. How to Write Command Script (Cont.)
Starting Off
Each script starts with a "shebang" and the path to the shell that you want the script to use, like so:
#!/bin/bashThe "#!" combo is called a shebang by most Unix geeks. This is used by the shell to decide which
interpreter to run the rest of the script, and ignored by the shell that actually runs the script. Confused? Scripts can be
written for all kinds of interpreters — bash, tsch, zsh, or other shells, or for Perl, Python, and so on. You could even
omit that line if you wanted to run the script by sourcing it at the shell, but let's save ourselves some trouble and add
it to allow scripts to be run non-interactively.
Scripting
1. How to Write Command Script (Cont.)
You might want to include a comment or two about what the script is for. Preface comments with the hash (#)
character:
#!/bin/bash
# A simple script Let's say you want to run an rsync command from the script, rather than typing it each time. Just
add the rsync command to the script that you want to use:
#!/bin/bash
# rsync script
Save your file, and then make sure that it's set executable. You can do this using the chmod utility, which changes a
file's mode. To set it so that a script is executable by you and not the rest of the users on a system, use "chmod 700
scriptname" -- this will let you read, write, and execute (run) the script -- but only your user. To see the results, run ls
-lh scriptname and you'll see something like this:
The first column of rights, rwx, shows that the owner of the file (jzb) has read, write, and execute permissions. The
other columns with a dash show that other users have no rights for that file at all.
Scripting
1. How to Write Command Script (Cont.)
Variables
The above script is useful, but it has hard-coded paths. That might not be a problem, but if you want to write longer
scripts that reference paths often, you probably want to utilize variables. Here's a quick sample:
#!/bin/bash
SOURCEDIR=/home/user/Documents/
DESTDIR=/media/diskid/user_backup/Documents/
There's not a lot of benefit if you only reference the directories once, but if they're used multiple times, it's much
easier to change them in one location than changing them throughout a script.
Scripting
2. How to Run Script Automatically on Crontab
Cron
Cron is a daemon that can be used to schedule the execution of recurring tasks according to a combination of the
time, day of the month, month, day of the week, and week.
To use the cron service, the vixie-cron RPM package must be installed and the crond service must be running. To
determine if the package is installed, use the rpm -q vixie-cron command. To determine if the service is running, use
the command /sbin/service crond status.
Scripting
2. How to Run Script Automatically on Crontab (Cont.)
The main configuration file for cron, /etc/crontab, contains the following lines:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin
Each line in the /etc/crontab file represents a task and has the following format: minute hour day month
dayofweek command
minute — any integer from 0 to 59
month — any integer from 1 to 12 (or the short name of the month such as Jan or Feb)
dayofweek — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as Sun or Mon)
command — the command to execute (the command can either be a command such as ls
/proc >> /tmp/procor the command to execute a custom script)
Scripting
2. How to Run Script Automatically on Crontab (Cont.)
For any of the above values, an asterisk (*) can be used to specify all valid values. For example, an asterisk for the
month value means execute the command every month within the constraints of the other values.
A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.
A list of values separated by commas (,) specifies a list. For example, 3, 4, 6, 8 indicates those four specific integers.
The forward slash (/) can be used to specify step values. The value of an integer can be skipped within a range by
following the range with /<integer>. For example, 0-59/2 can be used to define every other minute in the minute
field. Step values can also be used with an asterisk. For instance, the value */3 can be used in the month field to run
the task every third month.
Scripting
2. How to Run Script Automatically on Crontab (Cont.)
Any lines that begin with a hash mark (#) are comments and are not processed.
As shown in the /etc/crontab file, the run-parts script executes the scripts in
the /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories on an hourly, daily,
weekly, or monthly basis respectively. The files in these directories should be shell scripts.
Scripting
3. How to Schedule to Run Script
If a cron task is required to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to
the /etc/cron.d/ directory. All files in this directory use the same syntax as /etc/crontab. For examples.
# record the memory usage of the system every monday
10 4 1 * * /root/scripts/backup.sh
Scripting
3. How to Schedule to Run Script (Cont.)
Users other than root can configure cron tasks by using the crontab utility. All user-defined crontabs are stored in
the /var/spool/cron/ directory and are executed using the usernames of the users that created them. To create a
crontab as a user, login as that user and type the command crontab -e to edit the user's crontab using the editor
specified by the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab.
When the changes to the crontab are saved, the crontab is stored according to username and written to the
file /var/spool/cron/username.
The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every
minute for any changes. If any changes are found, they are loaded into memory. Thus, the daemon does not need to
be restarted if a crontab file is changed.
Scripting
3. How to Schedule to Run Script (Cont.)
The /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. The format of both access control files
is one username on each line. Whitespace is not permitted in either file.
The cron daemon (crond) does not have to be restarted if the access control files are modified. The access control
files are read each time a user tries to add or delete a cron task.
The root user can always use cron, regardless of the usernames listed in the access control files.If the
file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is
ignored.If cron.allow does not exist, users listed in cron.deny are not allowed to use cron.
Scripting
3. How to Schedule to Run Script (Cont.)
To start the cron service, use the command /sbin/service crond start. To stop the service, use the
command /sbin/service crond stop. It is recommended that you start the service at boot time.
Scripting
Q&A