signal handling with trap
Ben Scott
dragonhawk at gmail.com
Sun Feb 17 11:42:45 EST 2008
On Feb 17, 2008 7:35 AM, jesse lazar <jlazar at basicisp.net> wrote:
> i am trying to learn a little about shell programming for no real
> reason.
That's a perfectly good reason! :-)
> so i have taken to trying to make sense of the shell
> initialization process. i've got sys v init, or something, i don't
> really know. its a debian system.
Debian -- along with most other Linux distributions -- use System V
style initialization. It's named after System V [1], the release of
AT&T Unix that introduced it, back near the dawn of time [2]. Sys V
init is characterized by the use of individual scripts which control
the start and shutdown of services. You can find the scripts by
looking in "/etc/init.d/".
[1] That's a roman numeral ("five"), not a letter ("vee"), by the way.
[2] The actual dawn of time being 12:00:00 AM on 1 Jan 1970, of course.
Symbolic links are created to those scripts. The location and name
of those symlinks tell init(8) when to invoke them. Run the command
"ls -lad /etc/rc?.d" (omit the quotes) to see the directories
themselves. Take a look inside one of the directories. The initial
letter indicates "S" for "start" or "K" for "stop" (kill). The
number forces the ordering, so that services which depend on other
services start after the services they depend on.
If you're not familiar with Unix "man pages": The notation "init(8)"
means the entry for "init" in section 8 of the manual. The sections
are:
1 - Regular commands
2 - System calls (kernel functions called from userland programs)
3 - Library calls
4 - Special files (device nodes, found under /dev/)
5 - File formats and conventions
6 - Games
7 - Miscellaneous
8 - System administration
By default, the "man" gives you the first match it finds. You can
ask for a specific section by issuing a "man section page" command.
This is important because sometimes there are entries with the same
name in different sections. A good example is "printf", which is a
library call and a command which serves as a wrapper for the library
call. "man printf" gives me the command, but "man 3 printf" gives me
the library call.
> from what i gather:
> + the init program is called on after the boot process
> + init reads /etc/inittab
> + init executes the shell script /etc/init.d/rcS
Sounds right to me.
The inittab(5) file tells init(8) what to do. The file will include
a "sysinit" directive, which identifies the script to run once at
system boot, to initialize the system environment. On Debian, that
script is "/etc/init.d/rcS"; on Hat-ish systems, it will be
"/etc/rc.d/rc.sysinit".
Your inittab will also include references to "rc", which is a shell
script invoked to change the runlevel. On Debian, the full path is
/etc/init.d/rc (on Hat-ish systems, /etc/rc.d/rc). The "rc" script is
what actually invokes the service-specific scripts under the
/etc/init.d/ and /etc/rc?.d/ paths.
Runlevles are collections of services. Each runlevel gets a number.
Debian systems use runlevel 2 almost exclusively. Other
distributions use different runlevels. Red Hat and derivatives, in
particular, use runlevel 3 for normal operation without an XDM
(graphical login), and runlevel 5 for normal operation including an
XDM. On pretty much all systems, runlevel 0 is used for "shutdown
and halt", 6 for "shutdown and reboot", and for "single-user mode".
Single-user mode is used for trouble-shooting. It traditionally
starts only a single process -- a plain root shell, running on the
system console.
> i'm stuck here because i cannot figure out the usage of the trap
> command. what i find bewildering is that i don't get a manpage for trap
> when i type in 'man trap' using the root account. where do i go for
> reference on the use of the trap command?
"trap" is what is called a "shell built-in command". Most commands
exist as files somewhere in the filesystem (under /bin/ or /sbin/ or
/usr/bin/ or what-have-you). They're traditionally documented with
their own manual pages.
Some commands, however, are part of the shell, which is usually
bash(1) on Linux systems. Since they're part of the shell, they have
traditionally been documented in the shell's manual page. So invoke
"man bash" and look for the "trap" builtin command.
Some distributions provide man pages for shell builtins. On my
Fedora system, "man trap" gets me the man page titled
"bash_builltins(1)". "help trap" will also get you some help if
you're using bash with help compiled in.
Some commands exist as both shell builtins and external commands.
For example, echo(1) exists as an external command (/bin/echo) and as
a bash builtin command on my system. This can be confusing if you're
reading the docs for one variant but actually invoking the other!
> thus the line
> trap - INT QUIT TSTP
> is going to execute '-' when any of the signals INT QUIT TSTP are
> issued? that doesn't seem right to me... '-' doesn't seem to be a
> command or a list of commands.
A bare dash (-) for trap resets the signal handlers for those
signals to their default. So the above resets the signals INT, QUIT,
and TSTP to their default behaviors. See signal(7) for more
information on signals and their uses.
Hope this helps!
-- Ben
More information about the gnhlug-discuss
mailing list