Common Scenario Description: Many services on the test server, in order to avoid excessive resource overhead to set the original service to boot does not start, only to retain some of the necessary system services, so when the need to use which services need to be manually opened. Some services may have dependencies on other services, such as service a relies on service B, and service B relies on service C. The dependency problem can be resolved in a sequential manner, and exit execution if the dependency is checked for not satisfied.
Coding ideas:
(a) Why use a function?
1. You should consider using a function when there is duplication of code or when a task needs to be repeated several times with few modifications.
2. The function can process the arguments passed to it and return its exit status code (exit State) to the script for subsequent use.
(b) How the function parameters are passed?
The function refers to the arguments passed in by location (as if they were positional parameters (positional parameters)), such as $1,$2 and so on.
(c) How to deal with dependency relationships?
Resolves the dependency problem in sequential execution and exits execution if the dependency is not met.
Coding Example:
#!/bin/bashrequired_service_1=mysqlrequired_service_2=zabbix-serverrequired_service_3=zabbix-agentfunction help () {echo "Function: start\stop zabbix service and dependence, and check status "echo " Usage: $0 {start|stop|status|help} "}function check_service_ If_is_running () {service=$1service $SERVICE status >/dev/null 2>&1reval=$?if [[ $REVAL -eq 0 ]]; thenreturn 0elsereturn 1fi}function start_ service_if_is_stoped () {service=$1service $SERVICE start >/dev/null 2>&1check_ service_if_is_running $SERVICEREVAL =$?if [[ $REVAL -eq 0 ]]; thenecho $SERVICE is running...elseecho $SERVICE is not running, error code is $REVAL. exit 1fi}function stop_service_if_is_running () {service=$1service $SERVICE stop >/dev/null 2>&1check_service_if_is_running $SERVICEREVAL =$?if [[ $REVAL -eq 1 ] ; thenecho $SERVICE is stoped...fi}function status_service () {SERVICE=$1check_service_ if_is_running $SERVICEREVAL =$?if [[ $REVAL -eq 0 ]]; thenecho $SERVICE is running...elseecho $SERVICE is not running, error code is $REVAL. Exit 1fi}function start () {start_service_if_is_stoped $REQUIRED _service_1start_ service_if_is_stoped $REQUIRED _service_2start_service_if_is_stoped $REQUIRED _service_3}function stop () {stop_service_if_is_running $REQUIRED _service_3stop_service_if_is_running $REQUIRED _ service_2stop_service_if_is_running $REQUIRED _service_1}function status () {status_service $ required_service_1status_service $REQUIRED _service_2status_service $REQUIRED _service_3}case "$ " in start") start; Stop) stop;; status) status; *) helpexit 1;; Esac
Coding test:
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M01/5A/31/wKiom1T5GSzR8vkuAAE_QcC1nfg612.jpg" height= "265" />
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1617846
Linux manually start and stop multiple services with shell scripts