相关文章推荐
没有腹肌的围巾  ·  C 指针 | 菜鸟教程·  2 年前    · 
聪明伶俐的核桃  ·  向后台接口传递FormData格式的数组对象 ...·  2 年前    · 
自信的冰淇淋  ·  cv.imdecode和cv.imencod ...·  2 年前    · 
奔跑的鼠标垫  ·  【技能贴】Mysql-Workbench生成 ...·  2 年前    · 
Code  ›  ShellCheck: SC2009 – Consider using `pgrep` instead of grepping `ps` output.
https://www.shellcheck.net/wiki/SC2009
爱搭讪的消炎药
2 年前

Consider using pgrep instead of grepping ps output.

Problematic Code:

ps ax | grep -v grep | grep "$service" > /dev/null

Correct Code:

pgrep -f "$service" > /dev/null

Rationale:

If you are just after a pid from a running program, then pgrep is a much safer alternative. Especially if you are also looking for a pid belonging to a certain user or group. All of the parameters are in one command and it can eliminate multiple greps, cuts, seds, awks, etc.

If you want a field that's not the pid, consider doing this through ps + pgrep instead of ps + grep :

for pid in $(pgrep '^python$')
  user=$(ps -o user= -p "$pid")
  echo "The process $pid is run by $user"

This is more robust than ps .. | grep python | cut .. because it does not try to match against unrelated fields, such as if the user's name was pythonguru.

Exceptions

pgrep is not POSIX. Please ignore this warning if you are targeting POSIX userlands.

You can ignore this error if you are trying to match against something that pgrep doesn't support:

# pgrep does not support filtering by 'nice' value
 
推荐文章
没有腹肌的围巾  ·  C 指针 | 菜鸟教程
2 年前
聪明伶俐的核桃  ·  向后台接口传递FormData格式的数组对象_formdata传递数组对象_小男哥、的博客-CSDN博客
2 年前
自信的冰淇淋  ·  cv.imdecode和cv.imencode_51CTO博客_cv imdecode
2 年前
奔跑的鼠标垫  ·  【技能贴】Mysql-Workbench生成er图 - 简书
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号