python - Grep 和 pGrep 组成流程
问题描述
我根本不熟悉 bash,但我正在尝试制作一对能够检测到的脚本
- 如果一个程序正在运行。
- 如果 python/bash 脚本正在运行。
我的 nº1 代码是:
#!/bin/bash
X=$( pidof $1 )
if [ ${#X} -gt 0 ]
then
echo "$1 has already $X been started"
else
echo "$1 not started $X"
fi
效果很好,但不会检测到脚本,所以我做了第 2 次更改:
X=$( pgrep -f $1 )
起初 nº2 它似乎在工作,但是当我终止 python 脚本时,我仍然得到:
WebsocketServer has 5 length and it's already 11919 started
如果我这样做ps -ax
,进程的 PID 将无处可见。
但如果我写ps -ax | grep websocket
:
11921 pts/4 S+ 0:00 grep --color=auto websocket
如果我启动 python 脚本...
WebsocketServer has 11 length and it's already 11927 11935 started
怎么了?我是否以某种方式滥用命令?
编辑:忘了提到pgrep -f WebsocketServer
在终端中写入什么都不会返回,就像它应该的那样。
解决方案
问题是您的脚本的参数与您正在搜索的脚本名称相同,并且pgrep -f
正在查找该脚本。
您可以尝试以下技巧:将名称拆分为两个参数。
checkScriptAlive websocket Server
然后在脚本中,执行:
target="$1$2"
x=$(pgrep -f "$target")
推荐阅读
- python - 使用 Python 和 BeautifulSoup 从 HTML 中抓取数字
- reactjs - 从 React-Final-Form 中的字段验证函数访问所有表单值
- terraform - 如何使用 Terraform 和 CodeBuild 部署/定义 ECS(Fargate)实例
- sql - SQL Server:有什么方法可以防止在单个 UPDATE 语句中意外多次更新同一行?
- python - Anaconda Navigator 无法更新 Idex
- octave - GNU Octave 中的多边形偏移
- bigdata - 如何创建一个包含随机但合理的英文单词的大文件?
- flutter - 将 SharedPreferences 传递给 List 小部件
- mysql - 将两列与一列空连接,那为什么我得到空值?
- highcharts - 如何在 highcharts 堆积条形图上指定起点