How to get whole command line from a process?

  • How can I get the command arguments or the whole command line from a running process using its process name?

    For example this process:

    # ps
    PID   USER     TIME   COMMAND
     1452 root       0:00 /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/run/udhcpc.eth0.pid
    

    And what I want is /sbin/udhcpc -b -T 1 -A 12 -i eth0 -p /var/run/udhcpc.eth0.pid or the arguments. I know the process name and want its arguments. I'm using Busybox on SliTaz.

    Do you want to process the output from `ps` (not recommended) or are you looking for some alternative command to `ps` that will give you the output? What needs to be done when `ps` gives multiple lines of output? print all/first/last?

    Which operating system do you use?

    I edited my question. I know the process name and want its arguments. I'm using SliTaz (minimalistic linux) with Busybox.

    @Michael Have you tried `arg` like `command` as per @John answer?

    Yes but how to write it to a variable? I think of two command: PID=`pidof ` ps -o pid,args|grep $PID|tr -s " "|cut -d " " -f 2 But this way I don't get the args in one variable

    @Michael I edited my answer accordingly. You should be more specific about your needs when writing your question ;)

    Sorry, my fault. I don't have -p option using Busybox. Any further advices?

  • You could use the -o switch to specify your output format:

    $ ps -eo args
    

    From the man page:

    Command with all its arguments as a string. Modifications to the arguments may be shown. [...]

    You may also use the -p switch to select a specific PID:

    $ ps -p [PID] -o args
    

    pidof may also be used to switch from process name to PID, hence allowing the use of -p with a name:

    $ ps -p $(pidof dhcpcd) -o args
    

    Of course, you may also use grep for this (in which case, you must add the -e switch):

    $ ps -eo args | grep dhcpcd | head -n -1
    

    GNU ps will also allow you to remove the headers (of course, this is unnecessary when using grep):

    $ ps -p $(pidof dhcpcd) -o args --no-headers
    

    On other systems, you may pipe to AWK or sed:

    $ ps -p $(pidof dhcpcd) -o args | awk 'NR > 1'
    $ ps -p $(pidof dhcpcd) -o args | sed 1d
    

    Edit: if you want to catch this line into a variable, just use $(...) as usual:

    $ CMDLINE=$(ps -p $(pidof dhcpcd) -o args --no-headers)
    

    or, with grep :

    $ CMDLINE=$(ps -eo args | grep dhcpcd | head -n -1)
    

    @Michael replace `args` with `command` (or `cmd`).

    @Pandya Both `cmd` and `command` are aliases to `args`, this is probably unnecessary.

    I don't have a -p option using Busybox: Usage: ps [-o COL1,COL2=HEADER] [-T]

    You may use `grep` to catch the line you're actually interested in: `ps -eo args | grep dhcpcd | head -n -1`. I edited my answer.

    Oh, this looks good: ps -o args | grep | head -n 1

    The command line appears to be truncated at 4096 bytes in the middle of an argvalue. According to https://unix.stackexchange.com/questions/91561/ps-full-command-is-too-long this is a problem with older kernels.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM