Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
service script, revisited...
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
pr0phet
n00b
n00b


Joined: 18 Oct 2003
Posts: 8
Location: U.S., SE Charlotte

PostPosted: Sun Nov 02, 2003 1:50 am    Post subject: service script, revisited... Reply with quote

I seen quite a few posts in this forum concerning the ability to start and stop services vis a script rather than having to type /etc/init.d/PROGRAM.
Some have mentioned a script similar to Redhat's ever popular "service" script. So here is what I have come up with. :D

* UPDATED *

This has been simplified. Still is not as simple as pjp has pointed out, but Im wrapping my script around many other functions to give a single script for managing services in /etc/init.d. Eventually this will be able to manage the runlevels of services as well.

Usage:
Code:
service PROGRAM ARG

ARGS are as follows:
start,stop,restart,pause,zap,status,ineed,iuse,needsme,usesme,broken

or
Code:
service OPTION

OPTIONS are as follows:
status-all and show-runlevel

or to add or delete a program from the default runlevel...
Code:
service add PROGRAM default
service del PROGRAM default


Put this script somewhere in your path and make it executable chmod a+x

Code:
#!/usr/bin/env bash
#
# Wrapper to all scripts in /etc/init.d. with
# Eventually will manage runlevels aswell.

# Environment Variables.
INIT="/etc/init.d"
RUNSCRIPT="/sbin/runscript"
RCSTATUS="/bin/rc-status"
RCUPDATE="/sbin/rc-update"

# first check to see if we are root.
USERID=`id | awk '{print $1}'`
if [ "${USERID}" != "uid=0(root)" ]; then
        echo "You must be root to run this script"
        exit 1
fi
# Usage, how to use this script.
usage () {
        echo "Usage:"
        echo "`basename $0` PROGRAM { start|stop|restart|pause|zap|status|ineed|iuse|needsme|usesme|broken|runlevel }"
        echo "`basename $0` status-all     to show a status of all running services."
        echo "`basename $0` show-runlevel  to show what runlevel all services are installed in."
        echo "`basename $0` add PROGRAM default   to install a program into runlevel default."
        echo "`basename $0` del PROGRAM default   to remove a program from runlevel default."
        exit 2
}
# wrapper for '/bin/rc-status'
statusall () {
        ${RCSTATUS}
        exit 0
}
# wrapper for '/sbin/rc-update show'
showrunlevel () {
        ${RCUPDATE} show
        exit 0
}
# wrapper for '/bin/rc-status' continued
if [ "$1" = "status-all" ]; then
        statusall
        exit 0
# wrapper for '/sbin/rc-update show' continued
elif [ "$1" = "show-runlevel" ]; then
        showrunlevel
        exit 0
fi
# Add or delete services from runlevels.
if [ "$1" = "add" ]; then
        ${RCUPDATE} $1 $2 $3
        exit 0
elif [ "$1" = "del" ]; then
        ${RCUPDATE} $1 $2 $3
        exit 0
fi
# Show the runlevel of the current service.
if [ "$2" = "runlevel" ]; then
        showrunlevel | grep $1
        exit 0
fi
# Catch all, if no args given show usage.
if [ "$1" = "" ]; then
        usage
fi
# The actual service part. Thanks to riceboy50 :)
if [ -x "${INIT}/$1" ]; then
        ${RUNSCRIPT} ${INIT}/$1 $2
        exit 0
else
        echo "${INIT}/$1 not found!"
        echo
        usage
fi
# EOF


I tried using the script from Redhat, but, it didn't do what I wanted...
Please try this out and let me know how it works for you.
Questions, Comments and advice is always welcome. :wink:
_________________
"..this world is not your home, so don't make yourselves cozy in it. Don't indulge your ego at the expense of your soul." -1 Peter 2:11

"All that is not eternal is eternaly useless." -C.S. Lewis

V.Parsons aka pr0phet


Last edited by pr0phet on Sun Nov 02, 2003 9:22 am; edited 5 times in total
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 16757

PostPosted: Sun Nov 02, 2003 6:55 am    Post subject: Reply with quote

A more simple version is here.
_________________
Those who dream by day are cognizant of many things that escape those who dream only at night. --Poe
Back to top
View user's profile Send private message
pr0phet
n00b
n00b


Joined: 18 Oct 2003
Posts: 8
Location: U.S., SE Charlotte

PostPosted: Sun Nov 02, 2003 9:44 am    Post subject: Usage explained Reply with quote

What this script does...
You no longer have to call a service directly in /etc/init.d
You no longer have to run /sbin/rc-update to modify a services runlevel
You no longer have to run /bin/rc-status to show what services are running.
This one script does it all by making calls to those other scripts mentioned above. Giving you a single command to use to manage you services and runlevels.

The following code examples will use cupsd.

Add cupsd to the default runlevel
Code:
service add cupsd default


Remove cupsd from the default runlevel
Code:
service del cupsd default


To be sure which runlevel cupsd is installed in
Code:
service cupsd runlevel


Mamaging the cupsd service
Code:
service cupsd start
service cupsd stop
service cupsd restart
service cupsd zap
service cupsd status


Show the status of all running services
Code:
service status-all


Show the runlevel of all installed services
Code:
service show-runlevel

_________________
"..this world is not your home, so don't make yourselves cozy in it. Don't indulge your ego at the expense of your soul." -1 Peter 2:11

"All that is not eternal is eternaly useless." -C.S. Lewis

V.Parsons aka pr0phet
Back to top
View user's profile Send private message
floam
Veteran
Veteran


Joined: 27 Oct 2002
Posts: 1067
Location: Vancouver, WA USA

PostPosted: Sun Jan 04, 2004 10:02 pm    Post subject: Reply with quote

I'm using this for sure :) How hard would it be to do a bash_completion thing with this?
_________________
Think about your breathing.
http://floam.sh.nu
Back to top
View user's profile Send private message
ikaro
Advocate
Advocate


Joined: 14 Jul 2003
Posts: 2526
Location: Denmark

PostPosted: Sun Jan 04, 2004 10:27 pm    Post subject: Reply with quote

great one, thx for sharing .
_________________
linux: #232767
Back to top
View user's profile Send private message
floam
Veteran
Veteran


Joined: 27 Oct 2002
Posts: 1067
Location: Vancouver, WA USA

PostPosted: Sun Jan 04, 2004 11:00 pm    Post subject: Reply with quote

I made a small change so that non-root users can use this too, I changed the root check to:
Code:
# first check to see if we are root.
USERID=`id | awk '{print $1}'`
if [ "${USERID}" != "uid=0(root)" ]; then
        RUNSCRIPT="/usr/bin/sudo ${RUNSCRIPT}"
        RCUPDATE="/usr/bin/sudo ${RCUPDATE}"
fi
This assumes you have sudo installed and setup.
_________________
Think about your breathing.
http://floam.sh.nu
Back to top
View user's profile Send private message
jetblack101
n00b
n00b


Joined: 17 Jan 2005
Posts: 16

PostPosted: Wed Sep 21, 2005 10:42 pm    Post subject: Reply with quote

This looks like a great time saver and I am definitly going to check it out when I get home. Just wanted to say nice work and thanks for sharing :)
Back to top
View user's profile Send private message
jamapii
Guru
Guru


Joined: 16 Sep 2004
Posts: 536

PostPosted: Fri Sep 23, 2005 7:13 am    Post subject: Reply with quote

I think a "stop"/"restart" function that does the equivalent of other distributions' "/etc/init.d/$1 restart" would be useful, similar to (for the stop/pause case)

Code:

/etc/init.d/$1 $2    # stop or pause
if /etc/init.d/$1 status; then    # it is still marked as up
  killall $1   # wild guess
  /etc/init.d/$1 zap
fi


the restart case is harder to implement, because all the dependent services must be restarted.
Back to top
View user's profile Send private message
fratotec
n00b
n00b


Joined: 12 Oct 2005
Posts: 10

PostPosted: Wed Oct 12, 2005 9:55 pm    Post subject: service script Reply with quote

Hello,
I´m a redhat users, migrating to GenToo....
one function i´m missing in gentoo´s startup scripts is a more detailed status message.

For example, if a service ( fetchmail ) died, a
Code:
/etc/init.d/fetchmail status

returns * status: started...

in redhat / suse I had more clear response...
Code:
service fetchmail status    -> fetchmail ( pid 1204 ) is running...

or, if the process died, for some reason...
Code:
service fetchmail status   -> fetchmail dead but subsys locked 


Exist there any similar command in Gentoo os is the only way doing a
Code:
ps ax | grep fetchmail
to see that a process is really up and running ?

Thanks for attention...


Franz Baumgartner
Back to top
View user's profile Send private message
nianderson
Guru
Guru


Joined: 06 May 2003
Posts: 369
Location: Lawrence, KS

PostPosted: Tue Apr 25, 2006 2:12 am    Post subject: Reply with quote

floam wrote:
I'm using this for sure :) How hard would it be to do a bash_completion thing with this?


What do you mean by adding bash completion?
Back to top
View user's profile Send private message
BitJam
Advocate
Advocate


Joined: 12 Aug 2003
Posts: 2476
Location: Silver City, NM

PostPosted: Tue Apr 25, 2006 8:17 am    Post subject: Reply with quote

floam wrote:
I'm using this for sure :) How hard would it be to do a bash_completion thing with this?

This thread has a simpler script (it only handles the init.d services) but it also contains the bash_completion script. It might be a good starting point for making a bash_completion script for this program.
Back to top
View user's profile Send private message
nianderson
Guru
Guru


Joined: 06 May 2003
Posts: 369
Location: Lawrence, KS

PostPosted: Tue Apr 25, 2006 3:42 pm    Post subject: Reply with quote

Code:

# services completion by kromo <gentoo@kromo.de>
 
 _service()
 {
         local cur services
 
         COMPREPLY=()
         cur=${COMP_WORDS[COMP_CWORD]}
 
         # get available services
         services=`ls /etc/init.d/`
 
         if [ $COMP_CWORD -eq 1 ]; then
                 COMPREPLY=( $( compgen -W "$services" -- $cur ) )
         elif [ $COMP_CWORD -eq 2 ]; then
                 COMPREPLY=( $( compgen -W "start stop restart pause zap status \
                                  ineed iuse needsme usesme broken" -- $cur ) )
         fi
 
         return 0
 
 }
 
 complete -F _service service

stolen from
This thread
I just emerge bash-completion and dropped the second script in /etc/bash_completion.d/service and it service completes for the script in this thread.
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