How do I tell if a command is running or waiting for user input?
There's several approaches:
Try signalling end of input:Without superuser privileges it is hard to know what is going on underneath the hood. What can be done is to press Ctrl+d. Terminals and utilities in canonical mode send all available text to
read()
syscall upon receiving EOT signal bound to this key combination, and if there's no input -read()
returns negative exit status which most utilities accept as signal to exit. Therefore, if a utility is waiting for input it will exit upon receiving the key combination. Otherwise, the utility is either running tasks or is not written properly.Spying on syscalls:If you do have superuser privilege, you can run
strace
in another terminal to see what is currently being done. For that you need to find out PID of the program. For instance, in another terminal tab runpgrep -f firefox
which may 1234 as example and thensudo strace -f -p 1234
. If the output you see is stuck onread()
syscall, it means the command probably is waiting for input. Otherwise, if you see syscalls running by, then command is doing something else. See a related question for usage ofstrace
to also figure out if the long running command has exited.Use command's own methods: Among other things, such utilities as
dd
use signals. For instance, if you usekill -USR1 1234
(where 1234 is PID of the runningdd
command), it will print to stdout the amount of bytes currently processed. Of course, this requires knowing about such behavior of the command in the first place. The above two methods are more general, and don't require deep knowledge of each command's behavior ( though it's always best to know what you're actually executing - otherwise you risk running a command which may do damage ).
How to tell if a program is running or wanting user input
It depends on the program and how you invoke it.
Often but not always there will be a prompt, that indicates that the program is asking for input.
-
If you are not sure, you can check if the program's process is busy
uses CPU - use
top
orhtop
reads or writes - use
sudo iotop -o
And when the program has finished, you will see the shell's prompt.
Shellscript running
I had a shellscript that checks if a program is running, and now I have added the option -s
to make it run sudo strace -f -p <PID>
(according to Sergiy Kolodyazhnyy's answer) when a ... is found.
The shellscript uses
-
ps -ef
to find the majority of programs -
systemctl is-active --quiet
to find some programs -
and if you wish
strace
in anxterm
window.Install
xterm
if you want to usestrace
to watch the activity of a program.
Usage
$ ./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
You can install the shellscript running
into a directory in PATH
if you want easy access to it.
The shellscript code
#!/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"
Demo
Checking terminal windows in Lubuntu (LXTerminal started as x-terminal-emulator
and custom gnome-terminal
windows),
$ 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
There is a lot of activity as soon as the cursor is in the terminal window.
Starting grep
(waiting for input from /dev/stdin
)
$ grep -i --color 'hello'
asdf
Hello World
Hello World
Checking it
$ running -s grep
ps -ef: active:
'grep ...' is running
/usr/bin/xterm
checking pid=14982; quit with 'ctrl + c' in the xterm window
There is not much activity, and you can identify what is happening.