linux - 用于检查进程是否已在运行并在是时退出的 Shell 脚本

标签 linux bash shell jmeter scripting-language

我有一个带有方法 status()start() 的 shell 脚本。代码如下:

#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

现在当我运行这个脚本时,我必须检查它是否已经在运行,如果它正在运行我必须退出。让我知道如何执行此操作。

最佳答案

在函数开头加一个flag设置为1,在函数结束前设置为0,随意查询。

#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
}

另一种选择是写入外部文件并查询它。

关于linux - 用于检查进程是否已在运行并在是时退出的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38126347/

相关文章:

调用 Web 服务时出现 javax.xml.rpc.JAXRPCException : java. io.IOException

linux - rpmbuild - 在构建最终包之前删除源文件

macos - 为符号链接(symbolic link)设置图标

linux - 在 BASH/Shell 上连续排序数字

node.js - 将参数传递给来自标准输入的 Node 脚本

c - 如何打印 "apr_uint32_t"?

linux - 使用awk解析转换如下日志

Bash/JQ - 无法分配拉绳和分配变量

linux - 在 bash 中链接多个 chflags

python - RPi 上的 Raspbian - Python 脚本在 IDLE shell 中运行,但不在 cmd 中运行