加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 用于检查进程是否已在运行的Shell脚本,如果是,则退出

发布时间:2020-12-14 01:02:39 所属栏目:Linux 来源:网络整理
导读:我有一个 shell脚本,方法是status()和start().代码如下: #function to check the jmeter processes runningstatus(){ PID=$(ps -ef | grep jmeter|grep -v grep) echo "The jmeter processes running are: n$PID"}#function to run the .jmx file given by
我有一个 shell脚本,方法是status()和start().代码如下:

#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

现在当我运行这个脚本时,我必须检查它是否已经在运行,如果它正在运行,我必须退出.让我知道如何做到这一点.

解决方法

在函数开头添加一个标志并将其设置为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
}

另一种选择是写入外部文件并进行查询.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读