如何判断命令是正在运行还是在等待用户输入?

Gqq*_*big 17 command-line process io

在命令行上,我输入一个命令并按回车键。它不输出任何东西。我如何判断它是否正在运行但尚未输出,或者它正在要求用户输入?

Ser*_*nyy 18

有几种方法:

  1. 尝试发出输入结束信号:没有超级用户权限,很难知道幕后发生了什么。可以做的是按Ctrl+ d规范模式下的终端和实用程序在read()接收到绑定到此组合键的 EOT 信号后将所有可用文本发送到系统调用,如果没有输入 -read()返回大多数实用程序接受作为退出信号的否定退出状态。因此,如果实用程序正在等待输入,它将在收到组合键后退出。否则,该实用程序要么正在运行任务,要么未正确编写。

  2. 监视系统调用:如果您确实具有超级用户权限,则可以strace在另一个终端中运行以查看当前正在执行的操作。为此,您需要找出程序的PID。例如,在另一个终端选项卡中运行pgrep -f firefox,例如 1234,然后sudo strace -f -p 1234. 如果您看到的输出卡在read()syscall 上,则意味着该命令可能正在等待输入。否则,如果您看到系统调用正在运行,则命令正在执行其他操作。请参阅有关使用的相关问题strace还可以确定长时间运行的命令是否已退出。

  3. 使用命令自己的方法:除其他外,诸如dd使用信号之类的实用程序。例如,如果您使用kill -USR1 1234(其中 1234 是正在运行的dd命令的PID ),它将打印到标准输出当前处理的字节数。当然,这首先需要了解命令的这种行为。上述两种方法更通用,不需要深入了解每个命令的行为(尽管最好知道您实际执行的内容 - 否则您可能会运行可能造成损坏的命令)。


sud*_*dus 6

如何判断程序是否正在运行或需要用户输入

这取决于程序以及您如何调用它。

  • 通常但并非总是会有提示,表明程序正在请求输入。

  • 如果不确定,可以检查程序进程是否繁忙

    • 使用 CPU - 使用tophtop

    • 读取或写入 - 使用 sudo iotop -o

  • 当程序完成时,您将看到 shell 的提示。

脚本 running

我有一个 shellscript 来检查程序是否正在运行,现在我添加了一个选项-s,让它sudo strace -f -p <PID>在找到...时运行(根据 Sergiy Kolodyazhnyy 的回答)。

shellscript 使用

  • ps -ef 找到大多数程序
  • systemctl is-active --quiet 找一些程序
  • 如果你想strace在一个xterm窗口。

    安装xterm,如果你想使用strace来观看节目的活动。

用法

$ ./running
Usage:    ./running <program-name>
          ./running <part of program name>
Examples: ./running firefox
          ./running term                     # part of program name
          ./running dbus
          ./running 'dbus-daemon --session'  # words with quotes
          ./running -v term                  # verbose output
          ./running -s term                  # strace checks activity
Run Code Online (Sandbox Code Playgroud)

如果您想轻松访问它,您可以将 shellscript 安装running到一个目录中PATH

脚本代码

#!/bin/bash

# date        sign     comment
# 2019-02-14  sudodus  version 1.0

verbose=false
strace=false
if [ "$1" == "-v" ]
then
 verbose=true
 shift
fi
if [ "$1" == "-s" ]
then
 strace=true
 shift
fi

if [ $# -ne 1 ]
then
 echo "Usage:    $0 <program-name>
          $0 <part of program name>
Examples: $0 firefox
          $0 term                     # part of program name
          $0 dbus
          $0 'dbus-daemon --session'  # words with quotes
          $0 -v term                  # verbose output
          $0 -s term                  # strace checks activity"
 exit
fi

inversvid="\0033[7m"
resetvid="\0033[0m"
redback="\0033[1;37;41m"
greenback="\0033[1;37;42m"
blueback="\0033[1;37;44m"

runn=false
#tmpfil=$(mktemp)
tmpdir=$(mktemp -d)
tmpfil="$tmpdir/tmpfil"
vtfile="$tmpdir/vtfile"
vthead="$tmpdir/vthead"

# check by systemctl

systemctl is-active --quiet "$1"
if [ $? -eq 0 ]
then
 echo "systemctl is-active:"
 runn=true
fi

# check by ps

ps -ef | tr -s ' ' ' ' | cut -d ' ' -f 8- | grep "$1" | grep -vE -e "$0 *$1" -e "$0 *.* *$1" -e "grep $1" | sort -u > "$tmpfil"
#cat "$tmpfil"
if $verbose || $strace
then
 ps -ef |head -n1 > "$vthead"
 ps -ef | grep "$1" | grep -vE -e "$0 *.* *$1" -e "grep $1" | sort -u > "$vtfile"
fi

tmpstr=$(head -n1 "$tmpfil")
#echo "tmpstr=$tmpstr"
tmpess=$(grep -om1 "$1" "$tmpfil")
#echo "tmpess=$tmpess"
if [ "$tmpstr" == "$1" ] || [ "${tmpstr##*/}" == "$1" ] || [ "${1##*/}" == "${0##*/}" ] || [ "$tmpess" == "$1" ]
then
 echo "ps -ef: active:"
 runn=true
 if $verbose
 then
  cat "$vthead" "$vtfile"
 fi
elif test -s "$tmpfil"
then
 if $runn
 then
  echo "----- consider also ------------------------------------------------------------"
  if $verbose
  then
   cat "$vthead" "$vtfile"
  else
   cat "$tmpfil"
  fi
  echo "--------------------------------------------------------------------------------"
 else
  echo "----- try with: ----------------------------------------------------------------"
  if $verbose
  then
   cat "$vthead" "$vtfile"
  else
   cat "$tmpfil"
  fi
  echo "--------------------------------------------------------------------------------"
 fi
fi

if $runn
then
 echo -en "$greenback '$1"
 if [ "$tmpstr" != "$tmpess" ]
 then
  echo -n " ..."
 fi
 echo -e "' is running $resetvid"

 if $strace
 then
  which xterm
  if [ $? -eq 0 ]
  then
   pid=$(head -n1 "$vtfile" | sed 's/^ *//' | tr -s ' ' '\t' | cut -f 2)
   echo "checking pid=$pid; quit with 'ctrl + c' in the xterm window"
   xterm -title "'strace' checks '$1'" 2> /dev/null -e sudo strace -f -p $pid
  else
   echo "Please install 'xterm' for this function to work"
   exit
  fi
 fi
else
 inpath=$(which "$1")
 if [ "$inpath" == "" ]
 then
  echo -e "$redback no path found to '$1' $resetvid"
 else
  echo -e "$blueback '$1' is not running $resetvid"
 fi
fi
rm -r "$tmpdir"
Run Code Online (Sandbox Code Playgroud)

演示

检查 Lubuntu 中的终端窗口(LXTerminal 启动为x-terminal-emulator自定义gnome-terminal窗口),

$ running -v -s term 
----- try with: ----------------------------------------------------------------
UID        PID  PPID  C STIME TTY          TIME CMD
sudodus   2087  1384  0 13:33 ?        00:00:00 x-terminal-emulator
sudodus   2108  1269  0 13:33 ?        00:00:17 /usr/lib/gnome-terminal/gnome-terminal-server
--------------------------------------------------------------------------------
 no path found to 'term' 

$ running -v -s x-terminal-emulator
ps -ef: active:
UID        PID  PPID  C STIME TTY          TIME CMD
sudodus   2087  1384  0 13:33 ?        00:00:00 x-terminal-emulator
 'x-terminal-emulator' is running 
/usr/bin/xterm
checking pid=2087; quit with 'ctrl + c' in the xterm window
Run Code Online (Sandbox Code Playgroud)

很多活动一旦光标位于终端窗口。

在此处输入图片说明

开始grep(等待来自 的输入/dev/stdin

$ grep -i --color 'hello'
asdf
Hello World    
Hello World
Run Code Online (Sandbox Code Playgroud)

检查它

$ running -s grep
ps -ef: active:
 'grep ...' is running 
/usr/bin/xterm
checking pid=14982; quit with 'ctrl + c' in the xterm window
Run Code Online (Sandbox Code Playgroud)

没有太多活动,您可以确定正在发生的事情。

在此处输入图片说明