Into a dead end or into a cul-de-sac--shell script obtains PID of Dubbo service by name

Source: Internet
Author: User

In the afternoon, suddenly feel restless, probably because there is no good rest at noon. Laotaibuxiao is still doing the page integration thing, this is the work to do when the job. Then suddenly want to challenge the advanced point of the defect troubleshooting, the result is accidentally drilled a niu Jiao jian. The shell script that starts the Dubbo service always makes me feel uncomfortable, so a study, I do not want to do anything else, it must be smooth. Although bought the bird Brother Linux private food book, but did not seriously look, many things do not remember, had to degree Niang an afternoon, but the degree Niang out of the results of the quality is not high, and then changed Google a bit to search for high-quality articles. So it's clear that Bash shell and shell scripts are not the same thing, sometimes a little different.

But still a little bit of harvest, the whole understanding of the shell script some pits.

Suppose we're looking for the Tomcat instance in the tomcat-tuiguang/directory, look at the shell script: Ps-ef | grep tomcat-tuiguang/| Grep-v grep | awk ' {print $} ' is the first script to use Ps-ef | grep tomcat-tuiguang/Gets the process information that contains the tomcat-tuiguang/in the process information so that the results will include grep itself, so we need | Grep-v grep to exclude grep itself, and then print out the process to be searched through awk ' {print $} '. The above example simply prints out the process ID number and, of course, can be modified to kill the Tomcat process, as in the following script: Ps-ef | grep tomcat-tuiguang/| Grep-v grep | awk ' {print $} '  | sed-e "s/^/kill-9/g" | sh-

See the following article:

1. Original address: Bash Shell Gets the process PID, thanks to the original author for sharing.

2. Original address: Shell script obtains Tomcat's PID by name

Code one: called Script (dubb_ctl_jar.sh)

#!/bin/SHShow_usage () {Echo "Usage:xxx.sh Xxx.jar Ctrl"    Echo "**********************************************"    Echo "jar Filename-like This:base.jar"    Echo "ctrl-like This:start|stop|restart"    Echo "**********************************************"}  if[$#-ne2]; ThenShow_usage Exit1#else#    Echo "Hah: $"fi# Eligible for Dubbo service Operation # # JavaEnvExport Java_home=/library/java/javavirtualmachines/jdk1.7.0_79.jdk/contents/Homeexport jre_home= $JAVA _home/Jre#service_name is intercepted based on the file name passed-service-base-0.0.1-Snapshot.jar) # just need to the change this param namejar_name=$1app_name=${jar_name##*/}app_name=${app_name%%.jar*}service_dir=/code/apps/Dubbo_serversservice_name=$APP _name#jar_name=$SERVICE _name\.jarpid=$SERVICE _NAME\.PIDCD $SERVICE _dir Case " $" inchstart) nohup $JRE _home/bin/java-xms128m-xmx512m-jar $JAR _name >/dev/NULL 2>&1&Echo$! > $SERVICE _dir/$PIDEcho "= = = Start $SERVICE _name"        ;; Stop) #根据服务名称获取进程, exclude the PID p_id of the shell script itself=$(PS-ef |grep$SERVICE _name |grep '/bin/java'|grep-Vgrep|awk '{print $}')                if["$P _id"==""]; Then            Echo "= = = $SERVICE _name process not exists or stop success"        Else            Echo "= = = Begin Kill $SERVICE _name process, PID is: $P _id"            Kill`Cat$SERVICE _dir/$PID 'RM-RF $SERVICE _dir/$PIDKill-9$P _idfi        ;; Restart) $0StopSleep 2        $0StartEcho "= = = Restart $SERVICE _name"        ;; *) # # restart $0StopSleep 2        $0start;;EsacExit0

Code two: Start Script (dubbo_ctl_start.sh), filter file names based on criteria

#!/bin/SHDubbo_folder="/code/apps/dubbo_servers"EchoStart Dubbo_serversscript_src=$ (CD"$ (dirname"$0")";pwd) #cd $dubbo _foldercd $dubbo _folderfilelist=$(ls) for file inch$filelist Do    ifTest-f $file     Then                if[[ $file== *$". Jar"]] && [[$file!= *$"Lucene"*] && [[$file!= *$"Mail"*] && [[$file!= *$"SMS"*] && [[$file!= *$"SNS"* ]]              Then               #Echoinvoke another script to pass parameters, start the Dubbo serviceSH$script _src/dubbo_ctl_jar.SH$fileStartfi    #else    #    Echo$fileis the directoryfi DoneCD $script _src

Code three: Stop the script (dubbo_ctl_stop.sh), actually the same as the start script

#!/bin/SHDubbo_folder="/code/apps/dubbo_servers"Echostoping dubbo_servers#current pathscript_src=$ (CD"$ (dirname"$0")";pwd) #cd $dubbo _foldercd $dubbo _folderRM-rf./logs/*filelist=$ (LS) for file in $filelistdo if Test-f $file and then if [[$file = = *$ ". Jar"]            Then #echo call another script to pass the parameter, stop the Dubbo service SH $script _src/dubbo_ctl_jar.sh $file stop Fi #else # echo $file is the directory fidonecd $script _src

Into a dead end or into a cul-de-sac--shell script obtains PID of Dubbo service by name

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.