Note: This answer is an extension of artistoex's answer.
This command should display all currently running commands executed by the current user and started within the last 60 seconds:
ps x --sort -start_time -U YOURUSERNAME -o start,command | \
awk '$1>=recently&&$1<=now' \
recently=$(date --date='60 seconds ago' +%T) now=$(date +%T) | sed 1,1d
To use this command, click on an icon or menu item to execute a command and while the program that was just opened is still running, execute the above command. Remember to replace YOURUSERNAME
with the username of your current user.
Explanation:
ps
will display currently running processes. Explanation of ps
arguments:
x
: includes processes not executed through a terminal (actually a tty
). Adding -t '?'
would display only processes not associated with a terminal.
--sort -start_time
: sort the output by the time the process started (descending order)
-U YOURUSERNAME
: Replacing YOURUSERNAME with your username will show only processes executed by your user. This restriction can be removed if needed.
-o start,command
: Display two columns in the output: the start time of the process and the command that was executed
awk
is used here to only show processes executed recently. Explanation of awk
arguments:
$1>=recently&&$1<=now
: Restrict the output to processes that were executed within the last 60 seconds. To change this time frame, modify the next argument (recently
variable assignment)
recently=$(date --date='60 seconds ago' +%T)
: set the awk
variable recently
used in the previous argument to 60 seconds ago in HH:MM:SS
format (ps
time format).
now=$(date +%T)
: set the awk
variable now
to the current time (this is to exclude processes executed less than 24 hours ago that would look like they executed in the future)
I added sed 1,1d
to delete the first line of output because it will show the currently executing command, which is pointless to display.
Keep in mind: Using ps
to find out which process was executed will not work as expected for certain programs. For example, if you click on a Firefox shortcut but Firefox is already running, a new process will not be created and the start time of the old process will not be changed. However, this method does work fairly well for many programs.