Shell script to check if a process is running and if it is complete, if yes

I have a shell script with the status() and start() methods. Code below:

 #function to check the jmeter processes running status(){ PID=$(ps -ef | grep jmeter|grep -v grep) echo "The jmeter processes running are: \n$PID" } #function to run the .jmx file given by the user at run time start(){ echo "Please enter the file name .jmx extension" read file echo "Please enter the log file name .jtl extension" read log_file sh /home/ubuntu/apache-jmeter-3.0/bin/jmeter.sh -n -t $file -l $log_file & } while [ "$1" != "" ]; do case "$1" in start) jmeter_start ;; status) jmeter_status ;; *) echo $"Usage: $0 {start|status}" exit 1 esac shift done 

now when i run this script i have to check if it is running and if it is running i need to exit. Let me know how to do this.

+5
source share
2 answers

Add a flag at the beginning of the function and set it to 1, before the end of the function sets the value to 0, request it as you like.

 #function to check the jmeter processes running status(){ PID=$(ps -ef | grep jmeter|grep -v grep) echo "The jmeter processes running are: \n$PID" } #function to run the .jmx file given by the user at run time start(){ export start_flag=1 echo "Please enter the file name .jmx extension" read file echo "Please enter the log file name .jtl extension" read log_file sh /home/ubuntu/apache-jmeter-3.0/bin/jmeter.sh -n -t $file -l $log_file & export start_flag=0 } 

Another option is to write to an external file and request it.

+4
source

In fact, you already have most. You should be able to use the code from status , which receives the PID and just checks to see if it exists. If so, print some error and exit. Otherwise, do what you already have.

 start(){ PID=$(ps -ef | grep jmeter|grep -v grep) if [ -z $PID ]; then echo "Error: JMeter already running" exit fi echo "Please enter the file name .jmx extension" read file echo "Please enter the log file name .jtl extension" read log_file sh /home/ubuntu/apache-jmeter-3.0/bin/jmeter.sh -n -t $file -l $log_file & 

}

+1
source

All Articles