Linux Interview: How to make script run as service?

ajeetraina's picture

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.