Search code examples
linuxbashshellpswmctrl

how to get a complete command of X apps via 'wmctl' and 'ps'?


I'm working on a program that can query running X apps, save all the commands of running apps and them reopen them latter.

I encounter an issue. wmctctl can query the pid of Onlyoffice, for example the pid is 123. Then run ps -ef -q 123, I see that the CMD is ./DesktopEditors which should be a invalid command, because ./one_command only can work in special folder include file one_command.

I can get a complete command by running ps -ef -q $(pgrep -P 123).

Is there a straight way to get the complete command of Onlyoffice just via wmctl and ps?

If there is a better way to get all commands of X apps, please let me know. Thanks.


Solution

  • I suggest using ps -h -e -o pid,args command piped with a grep

    This should provide full command path with it arguments and options.

    For example find all running java programs with their arguments (might be extensive):

    ps -eo pid,args | grep java
    

    In your case I suggest a small awk script, that looks for the pid given as 3rd input field in current line:

    wmctrl -l -p|awk '{system("ps -h --pid "$3" -o args")}'
    

    Sample output

    nautilus-desktop --force
    /usr/libexec/gnome-terminal-server
    /usr/libexec/gnome-terminal-server
    

    update

    Transforming current directory ./ to to full path. Assuming ./ represent the current working directory. Add the following pipe.

    wmctrl -l -p|awk '{system("ps -h --pid "$3" -o args")}'|sed "s|^\./|$PWD/|"
    

    Find the script or program DesktopEditors in your computer, using find / -name "DesktopEditors".

    But I believe this is useless if you are trying to reverse engineer a web based application that requires some kind of a browser emulator.