当前位置:网站首页>Shell script controls the startup and shutdown of services - with detailed cases
Shell script controls the startup and shutdown of services - with detailed cases
2022-06-27 20:57:00 【I am fat tiger】
background
One of the functions involved in the automation script used in recent work is through shell Script to control process restart ( Because I have written before , But because the conclusion is not in place , The original notes cannot be found ) You can only search the general information on the Internet , Then I rearranged it according to my own understanding , I also reviewed the basic shell Script writing , Learn new things from the old !
Demand this time : Achieve one shell Script to control the start of the service , If the service has not been run before , This script allows the service to run ; If the service is already running , Then the script can restart the service !
The deployment environment
The services used in this article are tomcat Service as an example , After all, the service is relatively easy to download, install and deploy
tomcat Download address
https://downloads.apache.org/tomcat/tomcat-9/v9.0.62/bin/
tomcat Deploy
- Unzip the file first tar -zxvf apache-tomcat-9.0.62.tar.gz
- Entry directory , function tomcat cd apache-tomcat-9.0.62/bin
- function Start command : sh startup.sh Closing order : sh shutdown.sh
- Expected results + There may be a mistake
「 Expected results 」 adopt ps -ef|grep tomcat command , You can see tomcat Process information
「 The most common error reports 」 8080 The port is occupied , because tomcat The default port is 8080 At this time, you can use the lsof -i:8080, Find the pid, then kill -9 pid You can end the process , Rerun sh startup.sh that will do
restart tomcat Of shell Script
The final version code debugged by yourself is as follows
pid=$(ps -ef | grep tomcat|grep -v grep| awk '{print $2}')
if [ ! -z "$pid" ];then
echo " Currently running tomcat Service pid yes : ${pid}"
kill -9 ${pid}
echo -e " The current... Has ended tomcat service ~\n"
else
echo -e " Currently not running tomcat service !\n"
fi
sh startup.sh
echo -e "\n"
pid1=$(ps -ef | grep tomcat|grep -v grep| awk '{print $2}')
if [ ! -z "$pid1" ];then
echo " Restarted tomcat service ( * ̄▽ ̄)"
echo "tomcat New service pid yes : ${pid1}"
else
echo " restart tomcat Service failure , Please check if 8080 The port is occupied (>﹏<)"
fi
Name the above code restart.sh, On the tomcat Of bin Under the table of contents
- Test scenarios - Before 「 not 」 Start the service , Run the script at this time
- Test scenarios - Before 「 has 」 Start the service , Run the script at this time
this shell Code knowledge points
ps -ef The explanation of that long list of commands
ps -ef | grep tomcat|grep -v grep| awk '{print $2}'
- First ps -ef | grep tomcat Is to filter the current system , with tomcat Keyword for all processes
- Visible from above , Find out 2 Processes , One is expected tomcat process , The other is what we filter grep process . We need to take grep Process filtering , So use grep -v grep
- remarks : grep -v It means reverse search , such as grep -v grep Is to find no grep Row of field
- After the above operations , We only found one expected process , Our goal is to get the second column of data separated by spaces 27851 This process number , So I used awk command
- remarks : Hearsay Linux There are three swordsmen : grep, sed, awk. What we use here awk yes linux One of the three swordsmen !
- awk The usage scenario of is to obtain data separated by a separator , This separator defaults to a space . print $2 The representative takes the third 2 Columns of data , The process number of the current scenario 27851
shell Assign values to variables in and Value
It uses () and Variable These two usages
- Basic examples
a=$(date)
echo $a # Wed Apr 20 20:50:48 CST 2022
- tomcat Of shell Script
pid=$(ps -ef | grep tomcat|grep -v grep| awk '{print $2}') That is, the result of various operations pid Assign a value to pid Variable
shell Sentenced to empty
if [ ! -z "$pid" ];then This code uses the usage of judging null , If from pid The value of the variable is null , that if [ -z "$pid" ];then for True, add !, On behalf of the negative is False
pid=$(ps -ef | grep tomcat|grep -v grep| awk '{print $2}')
if [ ! -z "$pid" ];then
echo " Currently running tomcat Service pid yes : ${pid}"
kill -9 ${pid}
echo -e " The current... Has ended tomcat service ~\n"
else
echo -e " Currently not running tomcat service !\n"
fi
In this code block , If you can find tomcat Of pid, that if [ -z "$pid" ];then by Fasel, Go for True, Enter into then The following judgment statement , according to tomcat Of pid The end of the fall tomcat process
shell Other small details of
- 「 Double quotes and The difference between single quotation marks 」
Double quotes : Variables can be called inside , It is equivalent to using variables dynamically .if [ ! -z "$pid" ] It can be dynamically obtained pid The value of the variable is put into the judgment statement
Single quotation marks : Consider the contents as a string , Will not dynamically use the value of the variable inside . for example ,echo 'pid', The output is pid
- 「shell Output newline characters in the script 」
At first, I thought it was OK to do this directly echo "hello world\n", Discovery can't be , It has to be used this way echo -e "hello world\n". among -e The parameter represents the active escape character
边栏推荐
- [STL programming] [common competition] [Part 1]
- 展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
- Yyds dry goods counting SQL sub query
- Web APLS phase - Section 14 - local storage
- 分享下我是如何做笔记的
- Leetcode 989. Integer addition in array form (simple)
- muduo
- Flexible IP network test tool -- x-launch
- I haven't thought about the source for some time. After upgrading to the latest version 24, the data encryption problem is repeatedly displayed
- Zhongang Mining: the largest application field of new energy or fluorite
猜你喜欢
随机推荐
最佳实践:优化Postgres查询性能(下)
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
[STL programming] [common competition] [Part 2]
【STL编程】【竞赛常用】【part 2】
Necessary software tools in embedded software development
ABAP-CL_ OBJECT_ Collection tool class
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
云原生安全指南: 从零开始学 Kubernetes 攻防
[required reading for high-quality products] sub query of Oracle database in Linux system
Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled
Redis data structure
muduo
Practice of combining rook CEPH and rainbow, a cloud native storage solution
[STL programming] [common competition] [Part 1]
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
基于微信小程序的警局报案便民服务平台#毕业设计
Oracle 架构汇总
A distribution fission activity is more than just a circle of friends
Wechat IOS version 8.0.24 update release cache subdivision cleaning Online








