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
    • 最新
    • 标签
主页 / user-528934

Mathias Sven's questions

Martin Hope
Mathias Sven
Asked: 2025-03-18 07:41:57 +0800 CST

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

  • 5

设置是,我现在在当前目录下有两个文件夹,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 个回答
  • 33 Views
Martin Hope
Mathias Sven
Asked: 2024-11-15 06:25:39 +0800 CST

应用程序在使用 `>>` 时行为异常,替换行而不是附加行

  • 7

应用程序本身没有问题,我只是想知道周围的 Bash 的行为为何不如我预期,就像无论这个应用程序有什么行为,它都以我无法解释的原因的方式泄漏出去。

就上下文而言,应用程序是patsh,它是为 Nix(如包管理器)准备的,但本质上它需要一些输入文件,并根据一些规则重写它,然后将其保存到输出文件(这里是/dev/stdout),在这个上下文中,它不会重写任何东西,所以它本质上是cp。这是设置:

$ cat <<EOF > foo
1
2
3
4
5
6
EOF

$ cat <<EOF > bar
a
b
c
EOF

$ { patsh -f bar /dev/stdout; } >> foo  # Using braces to avoid any ambiguity...

$ cat foo  # It replaced instead of appending
a
b
c
4
5
6

$ cat <(patsh -f bar /dev/stdout) >> foo  # Does what I expected

$ cat foo
a
b
c
4
5
6
a
b
c

有人能解释这种行为吗?

bash
  • 1 个回答
  • 24 Views
Martin Hope
Mathias Sven
Asked: 2024-08-29 21:19:44 +0800 CST

smartctl 和设备类型不匹配

  • 6

长话短说,我试图更好地理解存储类型接口的不同标准,但 的输出smartctl让我有点困惑。这是我系统中的实际问题吗(就像另一篇帖子中看到的一些固件已过时一样)还是我误解了 的输出smartctl。

观察:

> sudo smartctl --scan
/dev/sda -d scsi # /dev/sda, SCSI device
/dev/nvme0 -d nvme # /dev/nvme0, NVMe device

我有一块 HDD 和一块 NVMe,但据我所知,HDD 不是 SCSI,除非它是“为什么我的 SATA 设备显示在 /proc/scsi/scsi 下? ”。但如果是,为什么我可以同时使用两者-d ata并-d scsi获取有关它的信息:

> sudo smartctl -d ata --info /dev/sda
smartctl 7.4 2023-08-01 r5530 [x86_64-linux-6.10.5] (local build)
Copyright (C) 2002-23, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Model Family:     Western Digital Scorpio Black (AF)
Device Model:     WDC WD5000BPKT-75PK4T0
Serial Number:    WD-WX11EC114329
LU WWN Device Id: 5 0014ee 6ad29b3f3
Firmware Version: 01.01A01
User Capacity:    500,107,862,016 bytes [500 GB]
Sector Sizes:     512 bytes logical, 4096 bytes physical
Rotation Rate:    7200 rpm
Device is:        In smartctl database 7.3/5387
ATA Version is:   ATA8-ACS (minor revision not indicated)
SATA Version is:  SATA 2.6, 3.0 Gb/s
Local Time is:    Thu Aug 29 14:09:19 2024 WEST
SMART support is: Available - device has SMART capability.
SMART support is: Enabled
> sudo smartctl -d scsi --info /dev/sda
smartctl 7.4 2023-08-01 r5530 [x86_64-linux-6.10.5] (local build)
Copyright (C) 2002-23, Bruce Allen, Christian Franke, www.smartmontools.org

User Capacity:        500,107,862,016 bytes [500 GB]
Logical block size:   512 bytes
Physical block size:  4096 bytes
LU is fully provisioned
Rotation Rate:        7200 rpm
Logical Unit id:      0x50014ee6ad29b3f3
Serial number:        WD-WX11EC114329
Device type:          disk
Local Time is:        Thu Aug 29 14:09:35 2024 WEST
SMART support is:     Unavailable - device lacks SMART capability.

根据两者的输出,ata显然是“正确”的类型,但却sudo smartctl -d ata --scan没有返回任何内容(与不同sudo smartctl -d scsi --scan)。

为什么看起来我可以同时使用ata和scsi来访问信息,但为什么却被检测scsi为--scan?

smartctl
  • 1 个回答
  • 16 Views
Martin Hope
Mathias Sven
Asked: 2023-10-29 01:43:40 +0800 CST

在系统设置事件中运行显示相关命令

  • 5

我正在开发一个 systemd/udev 设置,我想在上游共享它,但是我无法让它以非 hacky 的方式工作。本质上,我将此脚本作为 systemd 服务的执行程序:

      ICON="somepath/dslr-camera-white.png"

      function on-display() {
          local sdisplay=$(echo $XDG_SESSION_TYPE)
          if [ "$sdisplay" == "wayland" ]; then
          local display=":$(echo $WAYLAND_DISPLAY)"
          else
          local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
          fi
          
          local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)

          local uid=$(id -u $user)

          sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus "$@"
      }

      cleanup() {
          on-display notify-send -i $ICON "Disconnected" "The DSLR Camera has been turned off." --app-name="DSLR Webcam"
          trap - SIGTERM && kill -- -$$
      }

      trap cleanup SIGINT SIGTERM EXIT

      on-display notify-send -i $ICON "Connected" "The DSLR Camera has been turned on and it is ready to use." --app-name="DSLR Webcam"
      on-display yad --window-icon=$ICON --image=$ICON --no-buttons --title="DSLR Webcam" --notification --listen &

      output=$(v4l2-ctl --list-devices)
      line=$(echo "$output" | grep "Virtual Camera")
      vdevice=$(echo "$output" | sed -n "/$line/{n;s/^\t\+//p;}")
      gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 $vdevice

该服务的要点是当插入特定的 gphoto2 支持的相机时由 udev 规则启动,因此我有这些 udev 规则:

ACTION=="add", ATTR{idVendor}=="04a9", ATTR{idProduct}=="3218", RUN+="systemctl start dslr-webcam.service"
ACTION=="remove", ATTR{idVendor}=="04a9", ATTR{idProduct}=="3218", RUN+="systemctl stop dslr-webcam.service"

到目前为止,“好吧”,因为我已经阅读了使用RUNsystemd 服务的替代方案,但无论如何......这里的问题具体是对yad.

yad是一个允许您从 CLI 显示对话框的程序,我利用它来制作系统图标,因为我希望在相机处于活动状态时显示一个系统图标。

问题

与 不同notify-send,它可能在一些常见的套接字上工作,yad需要适当的XAUTHORITY设置才能工作,否则你会得到cannot open display: :0。在我的例子中,黑客解决方案是简单地将其设置为正确的 Xauthority,因为我使用的是 SDDM(显示管理器),它驻留在目录中/tmp,因此我可以将其添加到脚本中:

XAUTHORITY=$(ls /tmp/xauth*)

然后它就起作用了……但这很糟糕,它做了很多假设,事实上整个on-display函数似乎也是一个坏主意。如果我把它带到不同的系统,它可能不会工作,因为适当的 Xauthority 可能在无数的地方,而且我什至还没有尝试过 Wayland(我将把它留到以后)。

关于什么xauth

我以为我可以以某种方式xauth用来检索正确的 Xauthority,但情况似乎并非如此...这个脚本是一个系统服务,所以我xauth info返回Authority file: /root/.xauthWV7OfU,并以正确的用户身份运行它sudo -u $user xauth info给我Authority file: /home/myuser/.Xauthority,但这些都不起作用当给予yad. 正确的XAUTHORITY设置是由显示管理器设置的,所以我想我只能从它的子进程中获取它。

我也尝试了这个答案给出的所有方法,但是第一个方法不起作用,因为XAUTHORITY实际上不在系统环境中,而第二个方法(除了提到的陷阱)不起作用,它说xauth 文件位于/run/sddm/xauth_KvyuHd,但尝试使用它不起作用,因此它与 xauth 文件不同/tmp/xauth_FzoQqz

从与上一个相同的问题来看,这种方法似乎有效,但我不知道它的便携性如何。而且看起来仍然很老套。


作为用户服务运行

也许这是最有前途的一个,因为该脚本中没有任何内容阻止它作为用户运行(它也摆脱了on-display),我确实尝试了这种方法,而如果相机已插入并且我运行systemctl --user start dslr-webcam.service它就会工作正如预期的那样(包括yad),现在我对规则有疑问udev。我搜索了很多地方,包括这里,但我找不到如何从udev规则运行 systemd 用户服务,对我来说这也没有任何意义,如何udev知道要使用哪个用户?

systemd
  • 1 个回答
  • 18 Views
Martin Hope
Mathias Sven
Asked: 2023-10-21 06:30:44 +0800 CST

在 shell 脚本上安全获取 SUID 的最简单方法

  • 5

我有一个脚本,需要在其中安装覆盖层以使 OCI 容器能够访问已安装的只读目录,/nix/store就像它是可写的一样。

我想让非root/非wheel用户运行这个脚本,最简单的方法是什么?最初,我天真地尝试使用SUID,然后才意识到这些不起作用,这让我找到了一堆关于SUIDshell 脚本的危险的答案和文章,其中一篇解释了为什么仅仅将其包装在一些 C 程序中并不能解决问题非洁净环境的安全问题。

是否有一个简单的包装器/实用程序可以将脚本包装在其中来处理此类漏洞?

从用户环境中,我只需要一个 ENV,它刚刚传递到 docker 容器,但是,root 用户在其 bash 会话中将具有相同的 ENV 变量,因此,如果建议通过忽略所有用户环境而起作用,而是使用root 1,这对我的用例来说就很好了。

local temp_dir=$(mktemp -d)

mkdir -p {$temp_dir/store,$temp_dir/upper-store,$temp_dir/work-store}

# Create Overlay
sudo mount -t overlay overlay \
  -o lowerdir=/nix/store,upperdir=$temp_dir/upper-store,workdir=$temp_dir/work-store $temp_dir/store

# Do some sutff with the mounted overlay

sudo umount $temp_dir/store
rm -rf $temp_dir
shell-script
  • 2 个回答
  • 32 Views
Martin Hope
Mathias Sven
Asked: 2022-06-09 07:23:15 +0800 CST

当且仅当 SSH 隧道在端口 X 上处于活动状态时运行应用程序

  • 0

我在我的远程 Ubuntu 机器上运行一个代码服务器,现在我启动它的方法是

  1. ssh -t -t user@server "code-server"在一个终端上(-t -t我从这里得到的)
  2. ssh -N -L 8443:127.0.0.1:8443 user@server在第二个终端上
  3. 在浏览器上打开应用程序

然后当我想关闭它时,我只需关闭两个终端。我的问题是是否可以仅使用一个连接来执行此操作,目前我必须进行两次身份验证,因为这需要两个 ssh 会话,一个用于启动应用程序并在关闭时发送 KILL,另一个用于隧道。

ssh ssh-tunneling
  • 1 个回答
  • 24 Views

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