3

I am looking for a reliable way to get the full command line args passed to a process even when these are very long.

I do know about ps -f -p <pid> and /proc/<pid>/cmdline, both of which are usually up to the job. The problem is that both of these seem to truncate the command line args at 4096 chars, and I know for a fact that this one is longer ( It's a java application and the output truncates in the middle of the classpath string), and xargs --show-limits tells me that my system allows up to 2094184 argument length.

Is there a way to do this?

UPDATE: I was able to get the full command line args string by making a wrapper that I placed earlier in the PATH and logged the args to a file before calling the right binary with the same args.

#!/bin/bash

file=/tmp/commandtrap-$$.log

/usr/bin/echo "$@" >$file
/usr/lib/jvm/java-7-openjdk/jre/bin/java "$@"

This is, however, an ugly workaround that only works because I could stop and run the process.

Ideally, there should be a way to obtain the full command without the need to close the running process (or disturbing it in any way)

7
  • ps -p $PID -o args=
    – mikeserv
    Apr 3, 2015 at 17:19
  • Using unquoted $@ won't keep arguments correctly separated in the instance that one has spaces in. Change the java incovation to use /usr/lib/jvm/java-7-openjdk/jre/bin/java "$@" and it will work more accurately. Apr 3, 2015 at 17:28
  • @mikeserv, what OS is that for? I'm unable to use it on linux to see the full commandline. 'ps' with ARGS output truncates for me somewhere around 4k.
    – BowlOfRed
    Apr 3, 2015 at 17:30
  • @BowlOfRed - that's right. Add -wwf (I think those are the switches). 4k may be the limit though.
    – mikeserv
    Apr 3, 2015 at 17:40
  • 1
    Similar question: unix.stackexchange.com/questions/91561/…
    – BowlOfRed
    Apr 3, 2015 at 18:17

1 Answer 1

4

I don't believe there is a way in general. There might be for some specific OS. You said Unix/Linux, but perhaps you are interested in a solution for a particular OS?

As an example, Solaris stores the processes argument in two locations. One is an immutable buffer in the kernel associated with the process. But because it's a kernel buffer, the length is limited and it can be a truncated version of the command line.

The other location is ARGV[] in the process itself. This can be queried (such as via pargs) and will return untruncated contents. But since ARGV is part of the process memory, it can be modified at any time. The contents may not be the same as the command line that started the process.

A later user cannot be guaranteed of finding the original command line.


I just found Stéphane Chazelas's answer to a similar problem here: ps: full command is too long

It appears to include a method that attempts to decode ARGV[] on x86 ELF binaries, but I wasn't able to get data from it on a test case. I'm not sure why. But the technique seems reasonable.

1
  • I am interested mostly in a solution for linux.
    – Chikitulfo
    Apr 3, 2015 at 17:45

Not the answer you're looking for? Browse other questions tagged .