On command line, I typed a command and hit enter. It doesn't output anything . How do I tell if it's running and not yet output, or it's asking for user input?
5 Answers
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 ).

- 105,154
- 20
- 279
- 497
-
+1. Thanks for the
strace
method :-) But simpler methods are also useful (general or specific for each program). Some of them work without superuser privileges. Examples: check ifdd
is doing something and check whygrep --color asdf
is waiting silently. – sudodus Feb 14 '19 at 08:11 -
-
You don't need superuser privileges to debug a process owned by your user. Well, unless you haven't set the system up appropriately. – Ruslan Feb 14 '19 at 17:10
-
does the ubuntu terminal execution pause when some other window is clicked and becomes the new active window? – Shyam R Oct 02 '20 at 12:55
-
1@Shyam No, execution does not pause. If you experience such event something else is affecting your GUI. I'd suggest posting a question with exact details of your situation and someone may answer it in more details – Sergiy Kolodyazhnyy Oct 02 '20 at 22:14
-
@SergiyKolodyazhnyy, later I found that the execution dint pause. But the execution just was at the same position for a long time, so I got slightly confused as to what was going on. But many a times previously I had experienced situations like when I select a portion of text on my terminal app in ubuntu. Then copy the text, the execution stops for a while. Then if I paste somewhere else and comeback and select this terminal app and only after I press ctrl+c, the further text appears outputted on the app. – Shyam R Oct 16 '20 at 14:50
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 programssystemctl is-active --quiet
to find some programsand 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.

- 46,324
- 5
- 88
- 152
-
Good mention of
iotop
, though CPU usage might not necessarily be an indicator if a process is busy. A program written in C and optimized might be using minimal CPU. Some of the indicators I've written in Python schedule a repeated task to run, so it might use CPU to update the indicator menu for a brief moment and then just sits there. – Sergiy Kolodyazhnyy Feb 14 '19 at 08:20 -
@SergiyKolodyazhnyy, Yes, you are right about that. The
strace
method is better, but maybe not necessary or not available. – sudodus Feb 14 '19 at 08:24 -
Agreed. I don't think it comes preinstalled with Ubuntu, and might be overkill. – Sergiy Kolodyazhnyy Feb 14 '19 at 08:27
If you're running the shell in a terminal, for example a terminal emulator or a typical ssh session, your shell has almost certainly enabled job control. This makes getting an answer to your question super easy in most cases.
Type Ctrl+Z to suspend the process and then bg
to continue it in the background, then type an empty line to the shell so it'll check whether the program got stopped by a signal.
If the process is trying to read from the terminal, it will immediately get a SIGTTIN
signal and will get suspended. (When job control is enabled, the system allows only one process at a time to read from the terminal.) The shell will report this. You can then type fg
to continue the process in the foreground, and then type input to be read by the program as normal.
mp@ubuntu:~$ sleep 30 # a program that is not reading from the terminal
^Z
[1]+ Stopped sleep 30
mp@ubuntu:~$ bg
[1]+ sleep 30 &
mp@ubuntu:~$
mp@ubuntu:~$
mp@ubuntu:~$ cat - # a program that is reading from the terminal
^Z
[1]+ Stopped cat -
mp@ubuntu:~$ bg
[1]+ cat - &
mp@ubuntu:~$
[1]+ Stopped cat -
mp@ubuntu:~$ jobs -l
[1]+ 3503 Stopped (tty input) cat -
mp@ubuntu:~$ fg
cat -
hi
hi
Some programs, such as editors, will either trap or ignore the signal generated by Ctrl+Z or put the terminal into a mode where control characters don't even generate signals. You'll need to use more advanced techniques in this case, such as using strace
to see if the process is doing read
, select
, poll
, etc.

- 1,009
Not sure if you still need this, but still a useful trick to know: if the program seems to exit without any output you can check if it is running in background by executing
ps -efa | grep "program_name"
Cheers!

- 149
Why not simply look at the column S (State) of the top command. If your program is waiting for input, there is a great chance it is sleeping and not running meaning top will output a S (for Sleeping this time) and not a R (for Running). Try this command as an example:
top -b -n 1 | sed -n '7,12p' | awk '{printf "%6s %-10s %-4s %-s\n",$1,$2,$8,$NF}'
PS1
prompt. – Prvt_Yadav Feb 14 '19 at 05:24cat
command. Just typecat
by itself and it will wait for input from stdin, but gives no prompt. Many other commands behave similarly because they expect input from either stdin or a file, but don't differentiate between different input sources (interactive terminal, pipe, file...). – Dave Sherohman Feb 14 '19 at 11:01