如何如果一个进程与在bash的grep运行测试? [英] How to test if a process is running with grep in bash?

查看:113
本文介绍了如何如果一个进程与在bash的grep运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令:

ps ax | grep my_application

它输出大串包括参与my_application之类的proccesses的端口。

Which outputs a large string including the port of the proccesses involved in my_application.

如果my_application之不运行它输出:

If my_application is not running it outputs:

3873 pts/0    S+     0:00 grep my_application

我需要一个条件来测试 ps的斧头输出| grep的my_application之 的情况下,my_application之2号出口仍在运行。

I need a condition to test the output of ps ax | grep my_application and do exit 2in case my_application is still running.

任何想法?

推荐答案

您可以添加括号来排除grep的过程:

You can add brackets to exclude the grep process:

ps ax | grep -q '[m]y_application' && exit 2

如果my_application之运行, PS AX 将与grep命令和模式一起打印my_application之类。 grep的理解 [M] 作为一个字符类,但它不会litteral匹配字符串[M]。印刷通过 PS AX ,所以grep的过程中被排除在外。

If my_application is running, ps ax will print my_application along with the grep command and pattern. Grep understands [m] as a character class, but it will not match the litteral string '[m]' printed by ps ax, so the grep process is excluded.

这篇关于如何如果一个进程与在bash的grep运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆