'How to show current command execution time in tmux status line
I'm wondering if I can get a current command execution time (while command is executing) shown somewhere in the tmux's status line?
Solution 1:[1]
The question is a bit old but as I came across this while researching this, here's the answer:
for pid in $(tmux list-panes -a -F '#{pane_pid}'); do
for child in $(pgrep -P $pid); do
ps -p $child -ho etime,comm,args;
done;
done
Breaking it down:
First, we get the "first PIDs" for every TMUX pane, change this to narrow down which panels you want to include in your results:
tmux list-panes -a -F '#{pane_pid}'
"First PIDs" mean the PID of the shell, so it'll most likely be your shell (Fish, ZSH, Bash etc). So now we use a for
loop to iterate over those PIDs, and for each PID we get the PIDs of processes spawned by that PID, i.e. their children, which is done, inside the loop, by:
pgrep -P $pid
Now we have the child PIDs for each pane, which we can pass to ps
to get data on the process. etime
is the elapsed time since the process started, comm
is the command, and args
are the actual arguments, including the actual path of the command being run:
ps -p $child -ho etime,comm,args
The -h
flag tells ps
to not print the header, while the -o
flag sets the output, in this case it's three columns, etime
, comm
and args
.
It's a bit messy as a one-liner, but works:
for pid in $(tmux list-panes -a -F '#{pane_pid}'); do for child in $(pgrep -P $pid); do ps -p $child -ho etime,comm,args; done; done
Or, if you use fish-shell like me, it gets a bit better, though it's still not as clean as I'd like:
for pid in (tmux list-panes -a -F '#{pane_pid}'); for child in (pgrep -P $pid); ps -p $child -ho etime,comm,args; end; end
Still, this does the job, although a bit hackishly. Though, keep in mind, this doesn't check whether the process is running in the foreground, so if you daemonize stuff (&
, using Ctrl+Z etc) you will get data for those too, filtering daemons, pipes or whatever may be possible but I wanted all the data so I didn't look into any of those.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fernando Cordeiro |