Pressing “c” toggles the COMMAND column between displaying the process name and the full command line.
When running ps with the -f option in PuTTY (to see the command corresponding to each process), lines which are longer than the terminal width are not fully visible (they are not wrapped on multiple lines).
You're too late. By the time that the typed command gets to Python your shell has already worked its magic. For example, quotes get consumed (as you've noticed), variables get interpolated, etc.
In a Unix environment, this is not generally possible...the best you can hope for is the command line as passed to your process.
Because the shell (essentially any shell) may munge the typed command line in several ways before handing it to the OS for execution.
Look at the initial stack layout (Linux on i386) that provides access to command line and environment of a program: the process sees only separate arguments.
You can't get the command-line as it was typed in the general case. On Unix, the shell parses the command-line into separate arguments and eventually execv(path, argv)
function that invokes the corresponding syscall is called. sys.argv
is derived from argv
parameter passed to the execve()
function. You could get something equivalent using " ".join(map(pipes.quote, argv))
though you shouldn't need to e.g., if you want to restart the script with slightly different command-line parameters then sys.argv
is enough (in many cases), see Is it possible to set the python -O (optimize) flag within a script?
There are some creative (non-practical) solutions:
On Windows the native CreateProcess()
interface is a string but python.exe still receives arguments as a list. subprocess.list2cmdline(sys.argv)
might help to reverse the process. list2cmdline
is designed for applications using the same
rules as the MS C runtime—python.exe
is one of them. list2cmdline
doesn't return the command-line as it was typed but it returns a functional equivalent in this case.
On Python 2, you might need GetCommandLineW()
, to get Unicode characters from the command line that can't be represented in Windows ANSI codepage (such as cp1252).
As mentioned, this probably cannot be done, at least not reliably. In a few cases, you might be able to find a history file for the shell (e.g. - "bash", but not "tcsh") and get the user's typing from that. I don't know how much, if any, control you have over the user's environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With