Created
June 29, 2013 20:07
-
-
Save jathanism/5892482 to your computer and use it in GitHub Desktop.
Enclosed please find code and man page of a bash+sed script that uses
update-rc.d to emulate the behavior of the IRIX/Red-Hat chkconfig tool
Try a few invocations with --dryrun to see exactly what it does --
that switch makes it print the generated commands rather than
executing them. | Author: Eric S. Raymond |
Source: https://lists.ubuntu.com/…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # chkconfig -- list, enable, and disable system services by runlevel | |
| # | |
| # By Eric S. Raymond, May 2007. Released under BSD license. | |
| # | |
| # This is an emulation of the IRIX chkconfig tool to run over Debian/Ubuntu | |
| # (it calls update-rc.d, the native tool, to do the actual work). Also | |
| # requires sed(1) and runlevel(8). | |
| # | |
| # I wrote this because (a) a significant number of third-party packages | |
| # depend on chkconfig, and (b) as an ex-Red-Hat user, I'm used to this | |
| # interface and want it handy. | |
| # Things one might want to configure | |
| RUNLEVELS="1 2 3 4 5 6 S" | |
| ONOFFDEFAULT=2345 | |
| START=20 | |
| STOP=80 | |
| currentlevel () { | |
| # Get the current run level | |
| set -- `runlevel` | |
| echo $2 | |
| } | |
| explode () { | |
| # Explode a sequence of digits into a space-separated list | |
| echo "$1" | sed -e 's/[0-9A-Z]/ &/g' | |
| } | |
| checkroot(){ | |
| # Privileged operation coming up, require caller to be superuser. | |
| if [ "$prefix" != echo -a "$EUID" -ne 0 ] | |
| then | |
| echo "chkconfig: you must be root to use this feature." | |
| exit 1 | |
| fi | |
| } | |
| parse_init_header() { | |
| # Parse an LSB INIT INFO header for default start and stop levels. | |
| { | |
| state=0 | |
| startlevels='' | |
| stoplevels='' | |
| status=1 | |
| while read line | |
| do | |
| #echo "Looking at" $line | |
| if expr "$line" : ".*BEGIN INIT INFO" >/dev/null | |
| then | |
| state=1 | |
| status=0 | |
| continue | |
| fi | |
| if expr "$line" : ".*END INIT INFO" >/dev/null | |
| then | |
| state=0 | |
| continue | |
| fi | |
| if [ $state = 1 ] | |
| then | |
| t1=`expr "$line" : '.*Default-Start:[ \t]*\(.*\)'` | |
| if [ "$t1" != "" ] | |
| then | |
| startlevels="$t1" | |
| fi | |
| t2=`expr "$line" : '.*Default-Stop:[ \t]*\(.*\)'` | |
| if [ "$t2" != "" ] | |
| then | |
| stoplevels="$t2" | |
| fi | |
| fi | |
| done | |
| } </etc/init.d/$1 | |
| return $status | |
| } | |
| clearlevels () { | |
| # Clear init scripts for $name at runlevels $* | |
| name=$1 | |
| shift | |
| for level in $*; do | |
| $prefix rm -f /etc/rc[${level}].d/[SK][0-9][0-9]$name | |
| done | |
| } | |
| chkconfig_list() { | |
| if [ $# == 0 ] | |
| then | |
| for service in /etc/init.d/* | |
| do | |
| # Ignoring files that aren't executable is a cheap way | |
| # to skip README and other similar cruft like .dpkg-dist. | |
| if [ -x $service ] | |
| then | |
| chkconfig_list `basename $service` | |
| fi | |
| done | |
| else | |
| printf "%-16s\t" $1 | |
| for i in $RUNLEVELS | |
| do | |
| status="none" # Other chkconfigs report 'off' here | |
| if [ -f /etc/rc$i.d/K[0-9][0-9]$1 ] | |
| then | |
| status=off | |
| fi | |
| if [ -f /etc/rc$i.d/S[0-9][0-9]$1 ] | |
| then | |
| status=on | |
| fi | |
| printf "$i:$status\t" | |
| done | |
| echo "" | |
| fi | |
| } | |
| chkconfig_add() { | |
| if parse_init_header $1 | |
| then | |
| exec $prefix update-rc.d $1 start $START $startlevels . stop $STOP $stoplevels . | |
| else | |
| echo "chkconfig: can't --add a service with no INIT INFO header." | |
| exit 1 | |
| fi | |
| } | |
| chkconfig_del() { | |
| exec $prefix update-rc.d -f $1 remove | |
| } | |
| chkconfig_level() { | |
| levels=$1 | |
| name=$2 | |
| directive=$3 | |
| priority=$4 | |
| if [ "$name" = "" ] | |
| then | |
| echo "chkconfig: requires a service name" | |
| exit 1 | |
| fi | |
| # Query the state of a service (doesn't require root). | |
| if [ "$directive" = "" ] | |
| then | |
| levels=${levels:-`currentlevel`} | |
| if [ -f /etc/rc${level}.d/S[0-9][0-9]${1} ] | |
| then | |
| exit 0 | |
| else | |
| exit 1 | |
| fi | |
| fi | |
| # Turn on, turn off, or reset a service. | |
| checkroot | |
| case $directive in | |
| on) levels=${levels:-$ONOFFDEFAULT} | |
| clearlevels $name $levels | |
| $prefix update-rc.d $name start ${priority:-$START} `explode $levels` . | |
| ;; | |
| off) levels=${levels:-$ONOFFDEFAULT} | |
| clearlevels $name $levels | |
| $prefix update-rc.d $name stop ${priority:-$STOP} `explode $levels` . | |
| ;; | |
| reset) if parse_init_header $name | |
| then | |
| startlevels=${startlevels:-$RESETDEFAULT} | |
| stoplevels=${stoplevels:-$RESETDEFAULT} | |
| clearlevels $name ${startlevels}${stoplevels} | |
| $prefix update-rc.d $name \ | |
| start ${priority:-$START} `explode $startlevels` . \ | |
| stop ${priority:-$STOP} `explode $stoplevels` . | |
| else | |
| echo "chkconfig: no INIT INFO header found" | |
| exit 1 | |
| fi | |
| ;; | |
| *) echo "chkconfig: unknown operation '$directive'"; exit 1 ;; | |
| esac | |
| } | |
| if [ "$#" = 0 ] | |
| then | |
| echo -en "usage: chkconfig --list [name]\n\ | |
| chkconfig [--dryrun] --add <name>\n\ | |
| chkconfig [--dryrun] --del <name>\n\ | |
| chkconfig [--dryrun] [--level <levels>] <name> [<on|off|reset>] [priority]\n" | |
| exit 0 | |
| fi | |
| prefix="" | |
| if [ "$1" = "--dryrun" ] | |
| then | |
| prefix=echo | |
| shift | |
| fi | |
| case $1 in | |
| --list) shift; chkconfig_list $1 ;; | |
| --add) checkroot; shift; chkconfig_add $1 ;; | |
| --del) checkroot; shift; chkconfig_del $1 ;; | |
| --level) shift; chkconfig_level $* ;; | |
| *) chkconfig_level "" $* ;; | |
| esac |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Man page here: https://gist.github.com/jathanism/5892485