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?
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?
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 runsudo 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
You can install the shellscript
running
into a directory inPATH
if you want easy access to it.The shellscript code
Demo
Checking terminal windows in Lubuntu (LXTerminal started as
x-terminal-emulator
and customgnome-terminal
windows),There is a lot of activity as soon as the cursor is in the terminal window.
Starting
grep
(waiting for input from/dev/stdin
)Checking it
There is not much activity, and you can identify what is happening.
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 typefg
to continue the process in the foreground, and then type input to be read by the program as normal.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 doingread
,select
,poll
, etc.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
Cheers!
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}'