Can you get full command line from process ID (including command line arguments, etc)?

Viewed 264

This question is in addition to the question asked here: https://unix.stackexchange.com/questions/163145/how-to-get-whole-command-line-from-a-process. On my system, the following command results in a PID (as expected):

CUDA_VISIBLE_DEVICES=4,5 python3 main.py 1> out.txt 2> err.txt &

Now, the methods in the stack exchange link above provide many solutions. However, when trying these solutions, I only receive the following information:

python3 main.py

Is there a way to return the entire command line "CUDA_VISIBLE_DEVICES=4,5 python3 main.py 1> out.txt 2> err.txt &", not just the portion "python3 main.py"?

1 Answers

No.

Assuming you're on a Linux system, you can find the individual bits, but you can't put it together.

Assume also that the process's PID is in $pid

The CUDA_VISIBLE_DEVICES=4,5 variable gets added to the environment of the python command. You can find it in /proc/$pid/environ but you can't tell which of those variables were specified on the command line: the user could have written

export CUDA_VISIBLE_DEVICES=4,5 
python3 main.py 1> out.txt 2> err.txt &

The file redirections are available in /proc/$pid/fd:

  • /proc/$pid/fd/1 is a symbolic link to out.txt
  • /proc/$pid/fd/2 is a symbolic link to err.txt

I don't know how to tell if a process is running in the background.


Since you're just interested in the environment: with bash

declare -A environ
while IFS='=' read -r -d '' var value; do
    environ["$var"]="$value"
done < /proc/$pid/environ

echo "process has CUDA_VISIBLE_DEVICE value ${environ[CUDA_VISIBLE_DEVICE]}"
Related