I have a long running bash
instance (inside a screen
session) that is executing a complex set of commands inside a loop (with each loop doing pipes, redirects, etc).
The long command line was written inside the terminal - it's not inside any script. Now, I know the bash process ID, and I have root access - how can I see the exact command line being executed inside that bash
?
Example
bash$ echo $$
1234
bash$ while true ; do \
someThing | somethingElse 2>/foo/bar | \
yetAnother ; sleep 600 ; done
And in another shell instance, I want to see the command line executed inside PID 1234:
bash$ echo $$
5678
bash$ su -
sh# cd /proc/1234
sh# # Do something here that will display the string \
'while true ; do someThing | somethingElse 2>/foo/bar | \
yetAnother ; sleep 600 ; done'
Is this possible?
EDIT #1
Adding counter-examples for some answers I've got.
About using the
cmdline
under/proc/PID
: that doesn't work, at least not in my scenario. Here's a simple example:$ echo $$ 8909 $ while true ; do echo 1 ; echo 2>/dev/null ; sleep 30 ; done
In another shell:
$ cat /proc/8909/cmdline bash
Using
ps -p PID --noheaders -o cmd
is just as useless:$ ps -p 8909 --no-headers -o cmd bash
ps -eaf
is also not helpful:$ ps -eaf | grep 8909 ttsiod 8909 8905 0 10:09 pts/0 00:00:00 bash ttsiod 30697 8909 0 10:22 pts/0 00:00:00 sleep 30 ttsiod 31292 13928 0 10:23 pts/12 00:00:00 grep --color=auto 8909
That is, there's no output of the ORIGINAL command line, which is what I'm looking for - i.e the
while true ; do echo 1 ; echo 2>/dev/null ; sleep 30 ; done
.
gdb
:print (char *)rl_line_buffer
. The current command in a sequence isprint (char *)the_printed_command
. You may alsocall history_builtin()
, but that will output on the tty of the bash process, so it might be less useful. – mr.spuratic Oct 03 '14 at 10:16