Axel Bregnsbo
Axel Bregnsbo

Reputation: 4191

Identify last active Linux bash process

On Linux (e.g. RHEL 6) how do I on a given host identify the pid that last did some computation ? More precisely: among my 59 bash shells (I love virtual desktops), I would like to identify the terminal where I latest executed a command.

I tried looking in /proc/<pid> for some of my bash terminals to see if there were e.g. a command history file, or virtual file with a usable date stamp. Did not find anything.

Upvotes: 0

Views: 135

Answers (1)

Socowi
Socowi

Reputation: 27215

You can define a PROMPT_COMMAND which is executed each time bash displays a prompt, that is, after every command. In the prompt command, write the current PID to a file. When looking for the bash process that finished the latest command, retrieve its PID from that file.

In your ~/.bashrc add

export PROMPT_COMMAND='echo $$ > /tmp/last_active_bash_pid'

The effect will take place when you restart all your terminals or when your source the file in each terminal (execute . ~/.bashrc).

You can test this interactively using watch -n0.3 cat /tmp/last_active_bash_pid to show the PID.

Upvotes: 1

Related Questions