Shell script to check if the process is already running and exit if yes
Asked Answered
S

2

6

I have a shell script with methods status() and start(). The code is 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 already running and if it is running I have to exit. Let me know how to do this.

Shotten answered 30/6, 2016 at 14:37 Comment(4)
Where is this check taking place? In the start function?Germanous
start does not run in the background; it starts a different process in the background then immediately exists.Ultramicrometer
@Germanous That is what I was trying to implement but couldn't do itShotten
@Ultramicrometer yes. I am running the jmeter command to execute the .jmx file in background.Shotten
E
4

Add a flag at the beginning of the function and set it to 1, before the end of the function set it to 0, query it however 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 would be writing to an external file and query it.

Emylee answered 30/6, 2016 at 14:41 Comment(2)
I didn't understand this. Can you explain what you meant by query itShotten
Either use $start_flag or read from the file you wrote to using cat.Emylee
G
1

You actually have most of it already. You should be able to use the code from status that gets the PID and just check if it exists. If it does, output 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 &

}

Germanous answered 30/6, 2016 at 14:48 Comment(4)
but I want to check it for that particular start() method. I have updated the question. Please checkShotten
What differentiates two start functions? It doesn't really matter if two invocations of start run at the same time, as long as you don't wind up with two instances of jmeter running in the background.Ultramicrometer
@Ultramicrometer yeah. but i don't want to run 2 start() at the same timeShotten
@Ultramicrometer I have edited the question please check and helpShotten

© 2022 - 2024 — McMap. All rights reserved.