Saturday, December 5, 2009

LinuxInterview: How to make script run as service?

Making a script run at boot time needs certain consideration and configuration.The Startup script are usually under /etc/init.d directory.
Some Admin think that by just putting the script under /etc/init.d will make it run as service.
No !! The Story is little different.

Say, I have a script called myscript which I want to run as service either:

/etc/init.d/myscript [restart|reload|start|stop]
or
service myscript start

But little more steps are usually needed.
I am talking about Red Hat Distribution but may run comfortably in other *nix too.

I have a script called myscript which contains:

#cat myscript

#!/bin/sh
service nfs stop

Being a Linux Admin, I will try to put it under /etc/init.d/ directory.
Try to run it as:

service myscript start

And It does run comfortably:

#service myscript start
Shutting down NFS mountd: [ OK ]
Shutting down NFS daemon: [ OK ]
Shutting down NFS services:

But when I try making this service run at boot time:

#chkconfig myscript on
service myscript does not support chkconfig

Hmm...Not working !!!
How to fix it????
So here is the complete solution:

(Assume the name of my script is myscript)

1 - Copy your script into /etc/init.d folder
2 - cd /etc/init.d
3 - chmod +x myscript
4 - Add these lines, including #, right after #!/bin/bash or #!/bin/sh:

# chkconfig: 2345 95 20
# description: Some description
# What your script does (not sure if this is necessary though)
# processname: myscript

5 - chkconfig –level 2345 myscript on

Try now !!!
It will work.

4 comments:

  1. Do you know what does 95 and 20 means while mentioning chkconfig in the startup script?

    It says:It means the start priority should be 95 and stop priority should be 20.

    Hope it adds up to your blog

    ReplyDelete
  2. Thank you so much it has been a wonderful support, now to make script run as service? is without a doubt very easy by using your guidance. Kudos

    ReplyDelete
  3. my system stalls while starting the service at boot time any suggestion ? I think the system needs a return from the script(service). other wise it thinks the script as a active command line program waiting for termination.

    ReplyDelete
  4. Great Post, I love to read articles that are informative and actually have good content. Thank you for sharing your experiences and I look forward to reading more.
    website design

    ReplyDelete