AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 792616
Accepted
Mathias Sven
Mathias Sven
Asked: 2025-03-18 07:41:57 +0800 CST2025-03-18 07:41:57 +0800 CST 2025-03-18 07:41:57 +0800 CST

procps 的 watch 与 ps 命令一起截断命令行

  • 772

设置是,我现在在当前目录下有两个文件夹,busybox-1.36.1和,并且我还有一个正在运行的procps-4.0.4服务。现在观察不同的行为:languagetoolDynamicUser=yes

图例:✅ 不截断;❌ 截断。

busybox 手表 & busybox ps ✅

./busybox-1.36.1/bin/watch './busybox-1.36.1/bin/ps aux | grep languagetool'
Every 2.0s: ./busybox-1.36.1/bin/ps aux | grep languagetool                                                                           2025-03-17 23:20:02

1026972 61534    39:36 java ...languagetool...
4053857 mathiass  0:00 ./busybox-1.36.1/bin/watch ./busybox-1.36.1/bin/ps aux | grep languagetool
4053882 mathiass  0:00 sh -c -- ./busybox-1.36.1/bin/ps aux | grep languagetool
4053884 mathiass  0:00 grep languagetool

busybox 观看并 procps ps ✅

./busybox-1.36.1/bin/watch './procps-4.0.4/bin/ps aux | grep languagetool'
Every 2.0s: ./procps-4.0.4/bin/ps aux | grep languagetool

languag+ 1026972  0.1 13.6 14468788 4464816 ?    Ssl  Mar04  39:38 java ...languagetool...
mathias+ 4057218  0.0  0.0   4676  2244 pts/32   S+   23:35   0:00 ./busybox-1.36.1/bin/watch ./procps-4.0.4/bin/ps aux | grep languagetool
mathias+ 4057222  0.0  0.0 231736  3788 pts/32   S+   23:35   0:00 sh -c -- ./procps-4.0.4/bin/ps aux | grep languagetool
mathias+ 4057224  0.0  0.0 230732  2520 pts/32   S+   23:35   0:00 grep languagetool

procps 观看 & procps ps ❌

./procps-4.0.4/bin/watch './procps-4.0.4/bin/ps aux | grep languagetool'
Every 2.0s: ./procps-4.0.4/bin/ps aux | grep languagetool

mathias+ 4056463  0.0  0.0 230836  3320 pts/32   S+   23:31   0:00 ./procps-4.0.4/bin/watch ./procps-4.0.4/bin/ps aux | grep languagetool
mathias+ 4056540  0.0  0.0 230836  1532 pts/32   S+   23:32   0:00 ./procps-4.0.4/bin/watch ./procps-4.0.4/bin/ps aux | grep languagetool
mathias+ 4056541  0.0  0.0 231736  3556 pts/32   S+   23:32   0:00 sh -c -- ./procps-4.0.4/bin/ps aux | grep languagetool
mathias+ 4056543  0.0  0.0 230732  2244 pts/32   S+   23:32   0:00 grep languagetool

procps 监视 & busybox ps ✅

./procps-4.0.4/bin/watch './busybox-1.36.1/bin/ps aux | grep languagetool'
Every 2.0s: ./busybox-1.36.1/bin/ps aux | grep languagetool                                                                nixos: Mon Mar 17 23:33:31 2025

1026972 61534    39:38 java ...languagetool...
4056826 mathiass  0:00 ./procps-4.0.4/bin/watch ./busybox-1.36.1/bin/ps aux | grep languagetool
4056831 mathiass  0:00 ./procps-4.0.4/bin/watch ./busybox-1.36.1/bin/ps aux | grep languagetool
4056832 mathiass  0:00 sh -c -- ./busybox-1.36.1/bin/ps aux | grep languagetool
4056834 mathiass  0:00 grep languagetool

问题

奇怪的是,在其他所有情况下,命令的行为都符合我的预期,除了尝试同时使用ps和watchfrom 时procps。有人知道这是怎么回事吗?

ps
  • 2 2 个回答
  • 33 Views

2 个回答

  • Voted
  1. Sotto Voce
    2025-03-18T13:42:05+08:002025-03-18T13:42:05+08:00

    似乎正在发生的行为是procps 手册页 讨论的行为,尽管它不在最明显的地方。在页面的一半或更下方,有一个名为“标准格式说明符”的部分,描述了与选项一起使用的关键字-o。该部分以表格形式列出了每个说明符单词、其标题以及它显示的信息的描述。

    要检查的特定选项是“ args”。的描述字段args相当长,但开头说输出是命令和参数。进一步深入描述我们发现:

      If ps can not determine display width, as when output is redirected
      (piped) into a file or another command, the output width is undefined
      (it may be 80, unlimited, determined by the TERM variable, and so on).
      The COLUMNS environment variable or --cols option may be used to
      exactly determine the width in this case.  The w or -w option may be
      also be used to adjust width.
    
    

    它的意思是 procps 尝试找出它输出到的终端窗口的宽度。无论你是否使用-o args,procps 总是会这样做。当它无法找出宽度时 - 通常是因为它的输出没有直接进入终端窗口 - procps 可能会选择你不喜欢的宽度。

    因此,截断很可能是将输出通过管道传输到命令的副作用grep。如上所示,w向 procps 选项添加如下内容:

    ./busybox-1.36.1/bin/ps auxw | grep languagetool
    

    可能会解决这个问题,或者通过COLUMNS将变量设置为更大的值来调用它,如下所示:

    COLUMNS=200 ./busybox-1.36.1/bin/ps aux | grep languagetool
    

    可能会更好。

    • 1
  2. Best Answer
    Stéphane Chazelas
    2025-03-18T15:07:08+08:002025-03-18T15:07:08+08:00

    通常,当输出没有到达终端时,procps'ps不会截断命令行,但除非$COLUMNS设置了环境变量,并且procps的实现watch确实根据它所运行的终端的宽度来设置它¹。

    但是在这里,您不需要运行(这里使用实际上被 busybox 忽略的ps | grep非标准API ),而只需执行以下操作:ps auxps

    watch 'pgrep -af languagetool'
    

    (或者watch -x pgrep -af languagetool使用 procps'watch来避免不必要的 shell 调用)。

    pgrep,将仅与完整-f“进程参数列表”匹配正则表达式,而不会报告其自身或其祖先(例如此处的命令)。watch

    请注意,虽然busybox确实有一个pgrep小程序,但它并不总是启用的。

    这里,为了仅在命令行上匹配而不包括sh、grep/awk和watch在 procps 和 busybox 实现之间可移植的命令ps,您可以执行以下操作:

    watch '
      COLUMNS=0 ps -o args= |
        awk "/languagetool/ && ! /exclude-me-please/"
    '
    

    ¹ ksh、zsh、bash 或 fish 等 Shell 也设置了$COLUMNS Shell变量(在 bash 的情况下,非交互时则不然),但默认不会将其导出到环境中。

    ² 或者尽可能多地获取,因为早期版本的 Linux 内核仅提供了前 4096 个字节。

    ³ 与空格连接,并对一些字符(如控制字符)进行转换,类似于ps -wwo args。

    • 1

相关问题

  • ps:格式(分隔符和宽度)

  • 如何在 ps aux 中查看未连接到终端的进程?

  • `ps` 显示出比 `htop` 更高的内存使用率

  • `ps aux`中的`I`(大写i)是什么意思?

  • ps -vxa 不对内存进行排序

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve