Hi.
I've recently digged a bit deeper into the boot phase of Debian and at one
point had the feeling, that /etc/init.d/skeleton could need some care.
Petter asked me to send this here (which I do now)... but consider that
this is really a draft,... so far I've only edited the beginning,... with
some documentation and notes.
Some "major" changes so far:
- Added meta comments describing how to edit the template, which can be
easily removed by e.g. grep -v "^#>" or so..
- Inserted much documentation on the template in the beginning.
This includes general notes, hint's on availability of programs and
"areas" of the filesystem.
I'll still have to add a note, mentioning that init-scripts are
conffiles, and must take care of packages being removed only but not
purged.
- Put the template into public domain. Makes sense IMO, to allow derived
scripts using the package's native license.
Any previous authors, please add you name and email.
- Changed shebang to the more common form without whitespace in beetween.
- Removed the section for the author. Doesn't this follow from the
copyright
file? Even if not, wouldn't it be better to put copyright stuff in the
end of
the file (no need to parse it, code is not cluttered by boring info).
- Added the canonical form of set -e.
QUESTION: Shouldn't we "forbid" other set options too? Especially
nounset.
- Changed the order of the default PATH.
- As even stable has lsb-base 3.2-20, I've removed the version dependency?
Questions:
- Experts, please have a look at #208010 and my comments/questions there.
This is especially interesting, whether we should (which I'd say so)
already use LSB exit codes in the init-script template.
- Shouldn't we move skeleton to /u/s/d/initscripts/examples or so? IMO
/etc/init.d/ should really just contain scripts, at best only scripts with
LSB init headers... but not examples or templates (the template is neither
a configuration file).
- /lib/init/vars.sh is not necessarily there,... should we add a not on
this?
Looking forward to your comments.
Cheers,
Chris.
#>Init-Script Template
#>
#>This is a template for init-scripts.
#>It partially focuses on init-scripts for services or daemons.
#>
#>Lines beginning with “#>” are meta-comments and should be removed in actual
#>init-scripts.
#>
#>
#>Notes:
#>• A facility can be any procedure (for example the initialisation of
#> devices, filesystems, et cetera), service or daemon.
#>• As far as possible and reasonable, init-scripts should adhere to the “Shell
#> Command Language” and to the programs both as described in the “Shell and
#> Utilities” volume of the “Single UNIX Specification Version 3” (see the
#> package “susv3”).
#> This also means to avoid extensions to the lanauge (for example “BASHisms”)
#> or to these programs (for example additions by GNU) as well as unportable
#> functionalities.
#>• When the init-system (for example “sysvinit”) starts (that is “after”
#> the kernel and optionally an initramfs image) only the “/-filesystem” is
#> guaranteed to be available.
#> This means for example that only the programs in “/bin” and “/sbin”, the
#> libraries in “/lib*” and the data in “/etc” might be available.
#> Therefore, if an init-script depends (directly or indirectly) on anything
#> that is not guaranteed to be on the “/-filesystem” (for example programs in
#> “/usr/bin” or “/usr/sbin”) it must usually require the facility “$remote_fs”
#> (“$local_fs” is not enough).
#> If it only depends (directly or indirectly) on parts (see the literature) of
#> “/var” being available the facility “$local_fs” might suffice.
#> If it only depends (directly or indirectly) on parts of special file
#> hierarchy areas like “/dev”, “/proc” or “/sys” being available “simpler”
#> facilities (see the literature) may exist that might suffice.
#>• It should be considered that filesystems may be mounted read-only.
#>• It is often possible to omit existence checks (for example for files or
# programs) if it is guaranteed that the objects are installed and available.
# An object is guaranteed to be installed, if its package is either essential
# or depended or pre-depended on by the package of the init-script.
# An object is guaranteed to be avaialble, if itself and its (direct and
# indirect) dependencies are located on filesystems being available at all
# times the object is required. This can be assured via init-script
# dependecies. Also see the notes above.
#>• Derivates of this template may use any license.
#>
#>
#>Literature:
#>• Debian Policy
#> • Chapter “9.3 System run levels and init.d scripts”
#> • Chapter “9.4 Console messages from init.d scripts”
#> • Chapter “10.4 Scripts”
#> See the package “debian-policy”.
#>• Filesystem Hierarchy Standard
#> See the package “debian-policy”.
#>• Linux Standard Base, Core Specification
#> • Chapter “20.2. Init Script Actions”
#> • Chapter “20.3. Comment Conventions for Init Scripts”
#> • Chapter “20.5. Run Levels”
#> • Chapter “20.6. Facility Names”
#> • Chapter “20.7. Script Names”
#> • Chapter “20.8. Init Script Functions”
#> See at “http://refspecs.freestandards.org/lsb.shtml”.
#>• Debian Reference
#> • Chapter “3.5.6. The filesystem”
#> See the package “debian-reference”.
#>• Debian Wiki
#> • Article “LSBInitScripts”
#> See at “http://wiki.debian.org/”.
#>
#>
#>License:
#>Copyright © 2010, Christoph Anton Mitterer <[email protected]>.
#>All rights reserved.
#>
#>
#>This template is in the public domain.
#!/bin/sh
#>Notes:
#>• “Unnecessary” or “empty” keywords (in the example init info section below:
#> “Should-Start”, “Should-Stop”, “X-Start-Before”, “X-Stop-After” and
#> “X-Interactive”) should be removed.
#>• The name of the facility and the filename of the init-script should be
#> usually the same.
#>• Explicitly adding the run levels “0”, “6” or “1” to the keyword
#> “Default-Stop” may be unnecessary because of the facilities “sendsigs” and
#> “killprocs”. This can improve the performance.
### BEGIN INIT INFO
# Provides: facility-name
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: A short description of the facility in a single line.
# Description: A long description of the facility and the actions of the
# init-script that may span over multiple lines.
# X-Start-Before:
# X-Stop-After:
# X-Interactive:
### END INIT INFO
#>Notes:
#>• Avoid to “set -o” “errexit” or “nounset” (or their respective equivalents).
#>• The shell variable “PATH” should only include paths that are guaranteed to
#> be available (see the notes above).
#initialise and secure the shell execution environment
unset -v IFS
PATH='/usr/sbin:/sbin:/usr/bin:/bin'
#############################################################################################################
#below here its not even draft status and I haven't looked at the code below much,... so ignore it for now ;)
#initialise general variables
initscript="$0"
action="$1"
facility_description='short description or name of the facility'
daemon_name=executable-filename
daemon_executable=/usr/sbin/"${daemon_name}"
daemon_arguments='--options values commands arguments'
daemon_pidfile=/var/run/"${daemon_name}".pid
#check for the daemon
[ -x "${daemon_executable_pathname}" ] || exit 5
#load the rcS(5) variables (for example “VERBOSE”)
. /lib/init/vars.sh
#load the init-script configuration file, if it exists
[ -r /etc/default/"${initscript}" ] && . /etc/default/"${initscript}"
#define the LSB init-functions
##Note: The package should depend on “lsb-base” to ensure that this file and
## “status_of_proc” are available.
. /lib/lsb/init-functions
##Note: Function that starts the daemon/service.
do_start()
{
##Return
## 0 if daemon has been started
## 1 if daemon was already running
## 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile "${PIDFILE}" --exec "${DAEMON}" --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile "${PIDFILE}" --exec "${DAEMON}" -- "${DAEMON_ARGS}" \
|| return 2
##Note: If necessary, add code here that waits for the process to be
## ready to handle requests from services started subsequently
## which require this one.
## As a last resort, sleep for some time.
return 0
}
##Note: Function that stops the daemon/service.
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile "${PIDFILE}" --name "${NAME}"
RETVAL="$?"
[ "${RETVAL}" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec "${DAEMON}"
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f "${PIDFILE}"
return "${RETVAL}"
}
##Note: Function that sends a SIGHUP to the daemon/service.
do_reload()
{
##If the daemon can reload its configuration without
##restarting (for example, when it is sent a SIGHUP),
##then implement that here.
start-stop-daemon --stop --signal 1 --quiet --pidfile "${PIDFILE}" --name "${NAME}"
return 0
}
case "$1" in
(start)
[ "${VERBOSE}" = 'yes' ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
(0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
(stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
(status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
##(reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
(restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting ${DESC}" "${NAME}"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
(*)
#echo "Usage: ${SCRIPTNAME} {start|stop|restart|reload|force-reload}" >&2
echo "Usage: ${SCRIPTNAME} {start|stop|status|restart|force-reload}" >&2
#invoke-rc.d, service
exit 3
;;
esac
exit 1
_______________________________________________
Pkg-sysvinit-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/pkg-sysvinit-devel