You are on page 1of 2

Initscript example

Initscripts often make use of case statements for starting, stopping and querying system services.
This is an excerpt of the script that starts Anacron, a daemon that runs commands periodically with
a frequency specified in days.
case "$1" in
start)
start
;;

stop)
stop
;;

status)
status anacron
;;
restart)
stop
start
;;
condrestart)
if test "x`pidof anacron`" != x; then
stop
start
fi
;;

*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1

esac

The tasks to execute in each case, such as stopping and starting the daemon, are defined in
functions, which are partially sourced from the /etc/rc.d/init.d/functions file. See
Chapter 11 for more explanation.

Summary
In this chapter we learned how to build conditions into our scripts so that different actions can be
undertaken upon success or failure of a command. The actions can be determined using the if
statement. This allows you to perform arithmetic and string comparisons, and testing of exit code,
input and files needed by the script.
A simple if/then/fi test often preceeds commands in a shell script in order to prevent output
generation, so that the script can easily be run in the background or through the cron facility. More
complex definitions of conditions are usually put in a case statement.
Upon successful condition testing, the script can explicitly inform the parent using the exit 0 status.
Upon failure, any other number may be returned. Based on the return code, the parent program can
take appropriate action.
Exercises
Here are some ideas to get you started using if in scripts:
1. Use an if/then/elif/else construct that prints information about the current month. The script
should print the number of days in this month, and give information about leap years if the
current month is February.
2. Do the same, using a case statement and an alternative use of the date command.
3. Modify /etc/profile so that you get a special greeting message when you connect to
your system as root.
4. Edit the leaptest.sh script so that it requires one argument, the year. Test that exactly
one argument is supplied.
5. Write a script called whichdaemon.sh that checks if the httpd and init daemons are
running on your system. If an httpd is running, the script should print a message like, "This
machine is running a web server." Use ps to check on processes.
6. Write a script that makes a backup of your home directory on a remote machine using scp.
The script should report in a log file, for instance ~/log/homebackup.log. If you don't
have a second machine to copy the backup to, use scp to test copying it to the localhost. This
requires SSH keys between the two hosts, or else you have to supply a password. The
creation of SSH keys is explained in man ssh-keygen.

7. Adapt the script from the first example case of exactly 90% disk space usage, and lower
than 10% disk space usage.
The script should use tar cf for the creation of the backup and gzip or bzip2 for
compressing the .tar file. Put all filenames in variables. Put the name of the remote server
and the remote directory in a variable. This will make it easier to re-use the script or to make
changes to it in the future.
The script should check for the existence of a compressed archive. If this exists, remove it
first in order to prevent output generation.
The script should also check for available diskspace. Keep in mind that at any given moment
you could have the data in your home directory, the data in the .tar file and the data in the
compressed archive all together on your disk. If there is not enough diskspace, exit with an
error message in the log file.
The script should clean up the compressed archive before it exits.

You might also like