Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
frontend to rc-update, rc-status, and /etc/init.d/*
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
safeness
Tux's lil' helper
Tux's lil' helper


Joined: 02 Jul 2004
Posts: 95
Location: Eastside, WA

PostPosted: Mon Nov 14, 2005 10:53 pm    Post subject: frontend to rc-update, rc-status, and /etc/init.d/* Reply with quote

I got tired of typing in /etc/init.d/script restart, etc. and so here's a bash script to simplify all that. Usage info is given when the program is run with no arguments.
Code:
#!/bin/bash
#   created by safeness < chillinator at gmail dot com >
#   simplified interface to rc-update rc-status and the init scripts
#
#
#   functions:
#
#   initscript -- used to stop, start, or restart init scripts
#   rcstatus -- used to show status of init scripts
#   rcupdate -- used to show available init scripts, add to and delete from runlevels

usage()
{
   echo "Usage:    service status [boot | default]"
   echo "              service [start | stop | restart | zap] <init script>"
   echo "              service [add | del] <init script> <runlevel>"
        echo "              service ls"
        echo "'Runlevel' can be either 'default' or 'boot'. Init scripts can be viewed by running 'service ls'."
        echo "When the 'ls' option is given the argument 'unused' only unused services are displayed."
   return 0;
}   
rcupdate()
{
   case "$1" in

   "ls" )
   if [ -z "$2" ]
   then
      exec rc-update show | grep -v "Runlevel" | awk {'print $1'}
   elif [[ $2 == "unused" ]]
   then
      exec rc-update show | grep -v Runlevel | grep -v default | grep -v boot
   else
      usage
   fi
   ;;

   "add" )
   if [[ -f "/etc/init.d/$2" && $3 == "default" || $3  == "boot" ]]
   then
      exec rc-update $1 $2 $3
   else
      usage   
   fi
   ;;

   "del" )
   if [[ -f "/etc/init.d/$2" && $3 == "default" || $3  == "boot" ]]
   then
      exec rc-update $1 $2 $3
   else
      usage
   fi
   ;;
   
   esac
   return 0;

}

rcstatus()
{
   case "$1" in
   
   "boot" )
   exec rc-status $1 ;;
   
   "default" )
   exec rc-status $1 ;;

   "" )
   exec rc-status ;;
   
   * ) usage ''
   esac
   return 0;
}

initscript()
{
   case "$1" in

   "stop" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec /etc/init.d/$2 $1
   else
      usage
   fi
   ;;

   "start" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec /etc/init.d/$2 $1
   else
      usage
   fi
   ;;

   "restart" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec /etc/init.d/$2 $1
   else
      usage
   fi
   ;;
   
   "zap" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec /etc/init.d/$2 $1
   else
      usage
   fi
   ;;

   esac
   return 0;
}

if [ -z "$1" ]
then
   usage
else                     

   case "$1" in

   "status" ) rcstatus $2 ;;

   "ls" ) rcupdate $1 $2 ;;

   "add" ) rcupdate $1 $2 $3 ;;

   "del" ) rcupdate $1 $2 $3 ;;

   "start" ) initscript $1 $2 ;;

   "stop" ) initscript $1 $2 ;;

   "restart" ) initscript $1 $2 ;;

   "zap" ) initscript $1 $2 ;;

   * ) usage ;;
   esac
fi

exit


Let me know if you guys have any problems with this or any suggestions on how to make it better.
_________________
"The freedom of opinion and the reasonable maintenance of it is not a crime and ought not to occasion injury." --Thomas Jefferson
Back to top
View user's profile Send private message
adaptr
Watchman
Watchman


Joined: 06 Oct 2002
Posts: 6730
Location: Rotterdam, Netherlands

PostPosted: Mon Nov 14, 2005 11:04 pm    Post subject: Reply with quote

One very simple suggestion: call the script "svc" instead of "initscript" - it saves much typing ;-)

I have used a one-liner called /usr/local/bin/svc for years now, and it contains just this:

Code:
#!/bin/bash
/etc/init.d/$1 $2

Sure, you can pretty it up to only accept existing initscripts - but I know what I'm doing, and I use it for speed, not to hold my hand.

This allows me to hit
Code:
svc exim restart

inside of 2 seconds :P

Same reason I have aliased "repquota -a" to just rpq - I hate typing, and 3 letters allows for more commands than I will use in a lifetime.
_________________
>>> emerge (3 of 7) mcse/70-293 to /
Essential tools: gentoolkit eix profuse screen
Back to top
View user's profile Send private message
safeness
Tux's lil' helper
Tux's lil' helper


Joined: 02 Jul 2004
Posts: 95
Location: Eastside, WA

PostPosted: Tue Nov 15, 2005 1:21 am    Post subject: Reply with quote

I called the script 'service' on my machine, but called one of the functions in it 'initscript' to clarify that it was using the init scripts. Basically I was kinda bored and wanted to brush up on my bash scripting skills, so that's why I'm using functions, case statements, etc. :-D
_________________
"The freedom of opinion and the reasonable maintenance of it is not a crime and ought not to occasion injury." --Thomas Jefferson
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 18163

PostPosted: Tue Nov 15, 2005 10:32 pm    Post subject: Reply with quote

Isn't this much easier?
_________________
Those who know what's best for us must rise and save us from ourselves.
Back to top
View user's profile Send private message
safeness
Tux's lil' helper
Tux's lil' helper


Joined: 02 Jul 2004
Posts: 95
Location: Eastside, WA

PostPosted: Tue Nov 15, 2005 11:44 pm    Post subject: Reply with quote

That definitely is more simple, but doesn't do as much. This can add and delete scripts from runlevels using rc-update, show the status of different runlevels using rc-status, and do everything else that script did. I was using a script similar to that one for a while, but this was intended to be an all-in-one kind of solution to save me some typing and make things a little easier.
_________________
"The freedom of opinion and the reasonable maintenance of it is not a crime and ought not to occasion injury." --Thomas Jefferson
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Wed Nov 16, 2005 12:45 am    Post subject: Reply with quote

One thing is incorrect in the script.

Code:
'Runlevel' can be either 'default' or 'boot'.


Those are the DEFAULT runlevels. Anyone can create another runlevel with any name of their choosing.
Back to top
View user's profile Send private message
safeness
Tux's lil' helper
Tux's lil' helper


Joined: 02 Jul 2004
Posts: 95
Location: Eastside, WA

PostPosted: Wed Nov 16, 2005 2:12 am    Post subject: Reply with quote

Fair enough. I've never messed with custom runlevels, so I wasn't sure how to incorporate that into the script. How would you suggest adding support for that?
_________________
"The freedom of opinion and the reasonable maintenance of it is not a crime and ought not to occasion injury." --Thomas Jefferson
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Wed Nov 16, 2005 2:40 am    Post subject: Reply with quote

safeness wrote:
Fair enough. I've never messed with custom runlevels, so I wasn't sure how to incorporate that into the script. How would you suggest adding support for that?


What you need to do is change your rc-update function.
change this:
Code:

rcupdate()
{
   case "$1" in

   "ls" )
   if [ -z "$2" ]
   then
      exec rc-update show | grep -v "Runlevel" | awk {'print $1'}
   elif [[ $2 == "unused" ]]
   then
      exec rc-update show | grep -v Runlevel | grep -v default | grep -v boot
   else
      usage
   fi
   ;;

   "add" )
   if [[ -f "/etc/init.d/$2" && $3 == "default" || $3  == "boot" ]]
   then
      exec rc-update $1 $2 $3
   else
      usage   
   fi
   ;;

   "del" )
   if [[ -f "/etc/init.d/$2" && $3 == "default" || $3  == "boot" ]]
   then
      exec rc-update $1 $2 $3
   else
      usage
   fi
   ;;
   
   esac
   return 0;

}


to:
Code:

rcupdate()
{
   case "$1" in

   "ls" )
   if [ -z "$2" ]
   then
      exec rc-update show | grep -v "Runlevel" | awk {'print $1'}
   elif [[ $2 == "unused" ]]
   then
      exec rc-update show | grep -v Runlevel | grep -v default | grep -v boot
   else
      usage
   fi
   ;;

   "add" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec rc-update $1 $2 $3
   else
      usage   
   fi
   ;;

   "del" )
   if [ -f "/etc/init.d/$2" ]
   then
      exec rc-update $1 $2 $3
   else
      usage
   fi
   ;;
   
   esac
   return 0;

}


And you'll need to fix your rc-status function as well
Change
Code:
rcstatus()
{
   case "$1" in
   
   "boot" )
   exec rc-status $1 ;;
   
   "default" )
   exec rc-status $1 ;;

   "" )
   exec rc-status ;;
   
   * ) usage ''
   esac
   return 0;
}


to
Code:
rcstatus()
{
   case "$1" in
   
   "" )
   exec rc-status ;;
   
   * )
   exec rc-status $1 ;;
   esac
   return 0;
}


You can allow rc-status to account for the runlevel not existing.
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Wed Nov 16, 2005 4:18 am    Post subject: Reply with quote

Now I know I'm nitpicking here but I think a lot of your case statements could be shortened alot using simpler if statements.

Code:
case "$1" in

   "status" ) rcstatus $2 ;;

   "ls" ) rcupdate $1 $2 ;;

   "add" ) rcupdate $1 $2 $3 ;;

   "del" ) rcupdate $1 $2 $3 ;;

   "start" ) initscript $1 $2 ;;

   "stop" ) initscript $1 $2 ;;

   "restart" ) initscript $1 $2 ;;

   "zap" ) initscript $1 $2 ;;

   * ) usage ;;
   esac


You begin to notice that most of those functions are actually calling the same function in the exact same way.

Intead of what you put there I'd do:
Code:
if [ $1 == "status"]
then rcstatus $2
elif [ $1 == "ls" ] || [ $1 == "add" ] || [ $1 == "del" ]
then rcupdate $1 $2 $3 ;
elif [ $1 == "start" ] || [ $1 == "stop" ] || [ $1 == "restart" ] || [ $1 == "zap" ]
then initscript $1 $2 ;
else usage ;
fi
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Wed Nov 16, 2005 6:51 am    Post subject: Reply with quote

Here's my edited version of the original script. Not to "toot my own horn" but I think mine uses a more consise, simpler syntax.

I also changed it so that whenever an outside exec is made in a function it returns the exit status given by that outside program and whenever the script is exiting (or displaying the "usage") due to user error, it exits with the value of 1 since a non-zero exit status means there was an error.

So here we go:
Code:
#!/bin/bash
#   created by safeness < chillinator at gmail dot com >
#   edited and simplified by slycordinator < cslycord at gamail.com >
#   simplified interface to rc-update rc-status and the init scripts
#
#
#   functions:
#
#   initscript -- used to stop, start, or restart init scripts
#   rcupdate -- used to show available init scripts, add to and delete from runlevels

usage()
{
   echo "Usage:    service status <runlevel>"
   echo "              service [start | stop | restart | zap] <init script>"
   echo "              service [add | del] <init script> <runlevel>"
        echo "              service ls"
        echo "'runlevel' can be any defined runlevel. In a default gentoo install there are 2 runlevels ('boot' and 'default')"
        echo "Init scripts can be viewed by running 'service ls'."
        echo "When the 'ls' option is given the argument 'unused' only unused services are displayed."
}
rcupdate()
{
        if [ $1 == "ls" ]
        then
                if [ -z "$2" ]
                then
                        exec rc-update show | grep -v "Runlevel" | awk {'print $1'}
                elif [[ $2 == "unused" ]]
                then
                        exec rc-update show | grep -v Runlevel | grep -v default | grep -v boot
                else
                        usage ; exit 1
                fi
        elif [ $1 == "add" ] || [ $1 == "del" ]
        then
                if [ -f "/etc/init.d/$2" ]
                then
                        exec rc-update $1 $2 $3
                else
                        usage ; exit 1
                fi
        else
                usage ; exit 1
        fi
        return $?
}

initscript()
{
        if [ -f "/etc/init.d/$2" ]
        then
                exec /etc/init.d/$2 $1
        else
                usage ; exit 1
        fi
        return $?
}

if [ -z "$1" ]
then
        usage
else
        if [ $1 == "status" ]
        then
                exec rc-status $2 ;
                return $?
        elif [ $1 == "ls" ]
        then
                rcupdate $1 $2 ;
        elif [ $1 == "add" ] || [ $1 == "del" ]
        then
                rcupdate $1 $2 $3 ;
        elif [ $1 == "start" ] || [ $1 == "stop" ] || [ $1 == "restart" ] || [ $1 == "zap" ]
        then
                initscript $1 $2 ;
        else
                usage ; exit 1
        fi
fi
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum