May 06, 2008 Archives

06-05-2008 17:13

Services command like Red Hat

http://www.linux-noob.com/forums/index.php?showtopic=1685

I liked using the service command in my Red Hat days, have always missed it on my Debian systems. Thought it would be possible to add it myself but never did look into it. well here it is also with an auto completion ability for the service name.

Create a script /usr/sbin/service

#!/bin/sh

set -e

/etc/init.d/$1 $2 $3
And make it executable
chmod 755 /usr/sbin/service

Then to add auto completion to this, create: /etc/bash_completion.d/service with this inside

# a function for the autocompletion of the service command.

_service_fn()
{
  # set some local variables
  local cur prev

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}

  #if only one completion or a option is being requested
  #then complete against various options
  if [ $COMP_CWORD -eq 1 ]; then
    COMPREPLY=( $( compgen -W "$(ls /etc/init.d)" $cur ))
  elif [ $COMP_CWORD -eq 2 ]; then
    COMPREPLY=( $( compgen -W "start stop restart pause zap status ineed iuse needsme usesme broken" $cur ))
  else
    COMPREPLY=( $( compgen -f $cur ))
  fi

  return 0
}

#setup the completion
complete -F _service_fn service
Done!

PS I have created a script to automate this process. Its located here:

add-service.sh


Posted by DaveQB | Permanent Link | Categories: IT