Shell Start-Stop script

Source: Internet
Author: User
Tags stop script shebang log4j

To list any process listening to the port 8080:

lsof-i:8080
To kill any process listening to the port 8080:

Kill $ (Lsof-t-i:8080)
or more violently:

Kill-9 $ (lsof-t-i:8080)

Https://stackoverflow.com/questions/11583562/how-to-kill-a-process-running-on-particular-port-in-linux

Centos7

Demand

Because the company has a script that is used to start the program, but each start to do the following two things manually:
① first query the previous process ID
② killed before the start of a new

The time has made me very irritable, so I think about the above to integrate the operation into the startup script.
The first thing I wrote was this:

#!/bin/ Bashecho  "Ggjob-search ..." pid= ' ps-ef|grep-w ggjob-search| Grep-v grep|grep-v Ggjob-search.sh|awk  ' {printf $} ' echo Span class= "hljs-string" > "having kill Pid: $pid" Kill-9  $pidnohup Java-djava.ext.dirs=lib-xms500m-xmx500m-dlog4j.configuration=file:/home/webuser /gogoal_platform/ggjob/config/log4j.properties-jar Ggjob-search.jar config/config.properties 2>&1 >>/dev/null & echo  "new pid:$!"        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Executed a bit, OK
Results:

ggjob-search...having kill pid:7018new pid:7209
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

Assuming that the process has already been manually killed, execute the above script:
Results hint:

... or kill -l [sigspec]
    • 1
    • 1

Although the following command is not affected, the program is still started. But it's not nice to always report this.

So I'm going to add if judgment

Shell if notation

If space [space to judge expression space];then
Else
Fi

Judgment on undefined variables

As an example:

if [ "x${pid}" != "x" ];thenecho "说明pid变量不为空"elseecho "变量pid为空或未定义"fi
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

Note that there if is no extra space on the line above, that is, the space is required, otherwise the syntax error is reported.

Finally, my adjusted script is:

#!/bin/bashEcho "Ggjob-search ..." pid= ' ps-ef|grep-w ggjob-search|grep-v grep|grep-v Ggjob-search.sh|awk Span class= "hljs-string" > ' {printf $ {} ' if [ "X${pid}"! =  "X"]; thenecho  "having kill Pid: $pid "Kill-9  $pid finohup java-djava.ext.dirs=lib-xms500m-xmx500m-dlog4j.configuration=file:/home/ Webuser/gogoal_platform/ggjob/config/log4j.properties-jar Ggjob-search.jar config/config.properties 2>&1 >>/dev/null &echo  "new pid:$!"             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

Brief explanation:
① executes first ps -ef|grep -w ggjob-search|grep -v grep|grep -v ggjob-search.sh|awk ‘{printf $2}‘ , finds the process we want to kill ID and assigns a value to the variable pid .
② is judged, if the process is not killed, execute kill -9 , Force kill process operation.
③ Background launcher (I am here to start the jar package), and finally print the startup new ID .

Note: Because my script name is similar to the query process name, grep It is also necessary to filter out the process that the script itself initiates temporarily ID .

Here are some tips to collect online:

判断变量a为空最好的方法是:[  -z string ]  [  -n  string  ]# 这种方式if [ -z $a  ];thenecho "${LINENO} a is null"fi# -z 如果string长度为零,则为真# -n 如果string长度非零,则为真
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

After my extensive testing:
The above judgments about undefined variables can also be written like this:

# 经过我的测试使用-n 不能判断未定义变量if [ ! -z ${pid} ];thenecho "说明pid变量不为空"elseecho "变量pid为空或未定义"fi
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

This is not a description: The variable is not defined, when using the -n judgment, its string length is not empty, when using the -z judgment, its string length is empty. (I'm not sure, either)

operator Description Example
-e filename True if filename exists [-e/etc/profile]
-D filename True if filename is a directory [-d/tmp/mydir]
-F filename True if filename is a regular file [-f/usr/bin/grep]

Not a list of

Reference links

Shell Common moves Encyclopedia of entry: http://www.imooc.com/article/1788

Shell variable undefined or null causes test statement error resolution

http://blog.csdn.net/u013066244/article/details/70768664

Shell Start-Stop 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.