I am executing a command via Python's subprocess module. How can I view the full command that was executed?
For instance, if I submitted ['ls', '-l']
, I would like an easy way to see 'ls -l'.
Asked
Active
Viewed 92 times
2
-
2Why not do `' '.join(my_list)`? You're calling subprocess so you already know the full command. – Simeon Visser Jan 22 '14 at 14:46
-
Sounds like you're trying to do the reverse of `shlex.split()`? – Dan Getz Jan 22 '14 at 14:47
-
See http://stackoverflow.com/questions/2692873/ and http://stackoverflow.com/questions/967443/ – Dan Getz Jan 22 '14 at 14:56
1 Answers
4
This should work.
ls = subprocess.Popen(['ls', '/tmp'])
cmd = file("/proc/%d/cmdline" % (ls.pid)).read()
See The /proc Filesystem for more info and ideas.
Note that some commands will ask you for privileges.
Of course you could (as Simeon has pointed out) apply join
to the list you passed to Popen
.

Raydel Miranda
- 13,825
- 3
- 38
- 60