The shell script generation service demonstrates the service start and stop process.

Source: Internet
Author: User



1, write the service script/root/bin/testsrv.sh, complete the following requirements





650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8A/90/wKiom1gz-oST82CyAAAqG5XhNMQ339.jpg "title=" Centos.jpg "style=" float:right;width:125px;height:34px "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "alt = "Wkiom1gz-ost82cyaaaqg5xhnmq339.jpg"/>



1, write the service script /root/bin/testsrv.sh, complete the following requirements


(1) Script acceptable parameters: Start, stop, restart, status
(2) If the parameter is not one of these four, prompted to use the format after the error exit
(3) If start: Create/var/lock/subsys/Script_name, and displays "Start successful"
Consider: What should I do if I have already started it in advance?
(4) If stop: Delete/var/lock/subsys/Script_name, and show "Stop Complete"
Consider: What should I do if I have stopped in advance?
(5) If restart, stop first, then start
Consider: If there is no start, how to deal with it?
(6) If the status, if the/var/lock/subsys/Script_nameFile exists, it displays
“Script_nameIs running ... "
If/var/lock/subsys/Script_nameFile does not exist, it displays "Script_name
Is stopped ... "
whichScript_nameFor the current pin name
(7) Disable the start of the service in all modes, and can be managed with chkconfig and service commands





Script tests are used for experimentation. (The following scripts are not all written as described in the topic)





#! / bin / bash
#chkconfig: 345 98 2
#descrption: Testing services
#script_name: testser
#Author: Li
. /etc/rc.d/init.d/functions #Call system function files (comment)
ser = ‘/ var / lock / subsys / script_shell’ #Simulate a service file
start () {
Ranch
if [-e $ ser]; then
echo "The service is already running" #Check if the file exists and print the corresponding content
else
touch $ ser #create file
action "service started successfully" #action built-in function with comments at the end of the article
fi
}

stop () {
if [-e $ ser]; then
rm -rf $ ser
action "service stop"
else
echo "service not running"
fi
}

restart () {

if [-e $ ser]; then
sleep 0.5 #interval (only used to simulate the service startup process)
stop
sleep 0.5
start
else
action "service not running" / bin / false
sleep 0.5
start
fi
}

status () {
if [-e $ ser]; then
echo -e "\ tThe service is running ..."
else
echo -e "\ tservice not running"
fi
}

case $ 1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Please enter the correct parameters (start | stop | restart | status)"
esac


chkconfig --add testser #generate it as a service

chkconfig tesetser off #can prevent the service from starting
#Results of the
[[email protected] _C6 ~] # service testser start
service started successfully [OK]
[[email protected] _C6 ~] # service testser stop
service stop [OK]
[[email protected] _C6 ~] # service testser restart
service not running [FAILED]
service started successfully [OK]
[[email protected] _C6 ~] # service testser start
The service is already running
[[email protected] _C6 ~] # service testser status
The service is running ...
[[email protected] _C6 ~] # 




This script uses/var/lock/sybsys/script_shell as the test service. Use it to simulate the process of starting and stopping a service.



Note:action: Prints a message and executes the given command, which invokes the Success,failure method based on the result of the command execution



If you would like to know your reference to the following Brother's blog.



Http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html



This article is from the "Fall" blog, please be sure to keep this source http://lxlxlx.blog.51cto.com/3363989/1875458



The shell script generation service demonstrates the service start and stop process.


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.