Linux Shell writes system service script

Source: Internet
Author: User

1Pre-work: source code for Apache installation. The installation directory is/usr/local/httpd2 3 Mission Requirements:4 1, available through service httpd start|stop|status|Restart command to control a service5 6 2, httpd service can boot from start7 8 Ideas:9 1, start, stop operation can directly invoke the source code installed HTTPD control program ApachectlTen 2to establish a Httpd.lock file when the service is started, and to delete the service when it is stopped One 3, the status operation detects whether the Httpd.lock file exists, the existence of a service has been started, does not exist to indicate that the service stopped A 4, create a corresponding function for each operation, make the call - 5, the restart operation calls the STOP function before calling the start function - 6, the control parameters of the service script pass the location variable $1Incoming, use case branch to identify, perform the appropriate action the 7, add chkconfig management parameters at the beginning of the script, define which run level starts, service start priority, service shutdown priority (let service boot, must be added), Description service description, process name -  - PS: I have seen the system service script, and found that it is more complicated than I write. Excuse me, I just learned the shell, the first time I write a system service script -  + The script is as follows: -  +#vim/etc/init.d/httpd A#!bin/Bash at#chkconfig:2345  -  -    //run level, start priority, shutdown priority -#processname: httpd//Process Name -#description: source httpd Server Daemon//Service Description -Prog=/usr/local/httpd/bin/apachectl//Control Program Path -Lock=/usr/local/httpd/httpd.lock//Lock file path -Start () {//Start function in $prog Start -         Echo "starting service ...." to         Touch$lock + }  -Stop () {//Stop function the $prog Stop *         Echo "stopping service ...." $         RM-RF $lockPanax Notoginseng }  -Status () {//Status Function the         if[-e $lock]; Then +             Echo "$ A service is running" A         Else the             Echo "The service has stopped" +         fi - }  $Restart () {//Restart function $ Stop -Start//Call the stop, start function directly, - }  the  Case " $" inch        //Case Branch structure matching, the positional parameter is called on the control parameter - "Start") WuyiStart//Call the start function the         ;;  - "Stop")//Call the Stop function Wu Stop -         ;;  About "Status")//Call the status function $ Status -         ;;  - "Restart")//Call the restart function - Restart A         ;;  +*)//other parameters correct usage of output script the         Echo "usage: $ start|stop|status|restart" -         ;;  $ Esac the  the Verification: the[[Email protected]/]# service httpd start the starting service .... -[[Email protected]/]# service httpd status in/etc/init.d/httpd Service is running the[[Email protected]/]# service httpd Stop the stopping service .... About[[Email protected]/]# service httpd status the/etc/init.d/httpd Service has stopped the[[Email protected]/]# service httpd Stop thehttpd (No PIDfile) not running + stopping service .... -[[Email protected]/]# service httpd restatus theUsage:/etc/init.d/httpd start|stop|status|RestartBayi[[Email protected]/]# Service httpd Restart thehttpd (No PIDfile) not running the stopping service .... - starting service .... -[[Email protected]/]# the  the[Email protected]/]# Chkconfig--list httpd thehttpd0: Close1: Close2: Enable3: Enable4: Enable5: Enable6: Close the[[Email protected]/]# chkconfig httpd off -[Email protected]/]# Chkconfig--list httpd thehttpd0: Close1: Close2: Close3: Close4: Close5: Close6: Close the[[Email protected]/]# chkconfig httpd on the[Email protected]/]# Chkconfig--list httpd94httpd0: Close1: Close2: Enable3: Enable4: Enable5: Enable6: Close the[Email protected]/]#

Linux Shell writes system service script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.