Init Scripts

Tobias Gasser lfs at
Mon Feb 8 17:50:04 PST 2010

> Does anyone know any good step-by-step primers that cover writing init
> scripts from scratch? 

most services just have a "start" and "stop" option. i'm used to add a
"restart" option like "${0} stop; sleep 3; ${0} start". but here the
trouble already beginns: services like apache have options to restart
with several flavours, thus i not only have "restart" but also
"graceful". at least apache keeps it simple: just use "apachectl -k

often a "status" option is usefull too, most scripts from lfs and blfs
have this option implemented (statusproc : have a look at the function
script which is included in almost every lfs/blfs initscript).

looking at my "sensors" script, i have "modprobe xyz" in front of
"sensors -s" to be sure to have the hardware-dependent modules loaded.

samba is nice. you probably want to load smbd PLUS the netbios-daemon
(nmbd). but what about winbind? put all together in one init-script, or
use 2 or 3 scripts one for each service?? your distro - your rules...

starting a service usually is easy. either the service runs in backgroud
by default, can be forced to background with an option or will be happy
with the "loadproc" function as provided in the functions-script. an
exeption i already mentionned above: sensors requires modules to be
loaded, and not all the modules will be loaded by udev.

stopping a service can be a pain. some services have tools (like apache:
apachectl -k stop). some are writing pid-files where you just can kill
the corresponding pid. some can be convinced to write a pidfile by an
obscure (or sometimes obvious) command-line option. the "killproc"
function can kill a service by pid or by path.

some are instantanous down. some require a little time to really shut
down. the squid proxy sometimes needs even minutes to perform a clean
shutdown. a kill returns withing milliseconds as the main service is
down, but to be shure squid has really finished, you have to wait until
all instances are gone...

some scripts have no stop or no start option (cleanfs makes no sense on
stop / halt and reboot have no job on start)

if you like it complicated, you even can implement dependency checks in
your scripts (samba can't run without ldap, imap requires ldap and
mysql...), but i just give the script a fitting number...

error-processing can be very interessting (or anoying??) too. some
services don't remove the pid-file on exit, some do. the "loadproc"
fails if the pid already exists. thus you have to remove the pid
manually after shutdown the service. to remove the pid when starting the
script is not a good idea, as some services might fail or even crash on
multiple starts. (i remove these pid-files in the cleanfs script: i just
implemented a /etc/sysconfig/removefiles)

thus: no short step-by-stop howto...

... but:

first i learned a lot by reading the scripts provided in the lfs and
blfs init-script tarball.

now i'm used to have a look at the provided scripts in the tarballs.
most packages to be run as a service have examples or even ready-to-use
scripts for some of the common distributions.

just start playing arround. feel free to ask if you have a problem with
a specific package / service. but as i tried to show you, there won't be
a general-purpose init-script.

there are some alternatives arround, but after reading their docs i will
stay with system-v init-scripts for a little longer...


More information about the blfs-support mailing list