Pages

Thursday, August 30, 2012

Add Custom Linux Service


Location of Service Program
/root
Program Name
mysvcd
Runlevel
235
Start Priority
(for dependency)
90
Stop Priority
(100 - Start Priority)
10

Add Script for Your Service
       vi /etc/init.d/mysvcd

 ATTENTION: The chkconfig and  description must be specified in the script 
(e.g. runlevel = 235, start priority= 90, stop priority= 10)

                #!/bin/bash
                # chkconfig: 235 90 10
                # description: This is mysvcd script.
\
                #                      more description
                prog=mysvcd

                start() {
                                #code for start service
                                echo "Start mysvcd!!" 

                                /root/$prog&
                }

                stop() {
                                #code for stop service
                                echo "Stop mysvcd!!"
                }

                status(){
                                #code for status check
                                echo "Status of mysvcd!!"
                }

                restart() {
                                #code for restart service
                                echo "Restart mysvcd!!"
                                stop
                                start
                }

                case "$1" in
                        start)
                                start
                                ;;
                        stop)
                                stop
                                ;;
                        status)
                                status
                                ;;
                        restart)
                                restart
                                ;;
                        *)
                                echo "Usage: $prog {start|stop|restart}"
                esac


Add Service
                chkconfig --add mysvcd

If you want to start service immediately, run the following command:
                service mysvcd start

No comments:

Post a Comment