The following is an example of adding a service called Watchcat:
1. Write a script to be used for the service command
Service commands are generally used as follows
Start:
$ service Watchcat Start
Stop it:
$ Service Watchcat Stop
Restart:
$ Service Watchcat Restart
So this watchcat script needs to accept at least three of these commands.
Probably as follows: $ cat Watchcat
#!/bin/BashfunctionStart_cat () {Echo "Cat Run ..."}functionStop_cat () {Echo "Cat Stop ..."} Case " $" inchstart) start_cat;; stop) Stop_cat;; restart) stop_cat Start_cat;; *) Echo "Usage:start | stop | restart" ;;Esac
View Code
Note: This script is not a final script and cannot be used as a service
2, also need to let this script be chkconfig support
In fact, this is very important, that is, just add a few lines of comments on the script, these lines are just used for chkconfig. That is, when Chkconfig sees these lines of comments, it does not take these lines as comments.
The complete script is as follows:
1#!/bin/Bash2 #3 # Watchcat Start/stop Cats4 #5 # chkconfig:12345 6 # Description:start/stop Cats7 #8 # # # BEGIN INIT INFO9 # Description:start/stop CatsTen # # # END INIT INFO One A functionStart_cat () - { - Echo "Cat Run ..." the } - - functionStop_cat () - { + Echo "Cat Stop ..." - } + A Case " $" inch at start) - Start_cat - ;; - - stop) - Stop_cat in ;; - to restart) + Stop_cat - Start_cat the ;; **) $ Echo "Usage:start | stop | restart"Panax Notoginseng ;; - Esac
Now this script can be officially used.
The 5th line of the above code is a must, this is for chkconfig to see.
The 5th line of 12345 indicates that the WATCHCAT program needs to be started at the time the system is running at 1, 2, 3, 4, 5. 80 and 90 means to establish s80xxxxx and k90xxxxx linked files under the rc1.d/~rc5.d/directory.
3. Configuring Watchcat Service Support
First add executable permissions to Watchcat:
chmod 775 Watchcat
Copy the script to the/etc/init.d/directory
CP watchcat/etc/init.d/CPfile '/etc/init.d/watchcat'Permission denied sudocp watchcat/etc/init.d/
At this point, you can use the service to control it, as follows:
$ service Watchcat start Cat run ... $ service watchcat stop Cat Stop ...
4, but the egg, this is only the completion of the manual control phase, but also need to boot
Check it out first.
$ chkconfig--list Watchcatservice Watchcat supports Chkconfig, but isn't referencedinchAny RunLevel (run'chkconfig--add Watchcat')
We didn't add this service called Watchcat $ chkconfig--Add Watchcatyou DoNot has enough privileges to perform this operation.$sudoChkconfig--Add Watchcat
Check the results after adding$ chkconfig--List Watchcatwatchcat0: Off1: On2: On3: On4: On5: On6: Off
So it's done.
Citations:
Http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html
Http://www.linuxidc.com/Linux/2015-01/111438.htm
Linux adds service services and automatically adds Chkconfig boot level