Home Linux Commands How To Find When A Command Is Executed In Linux

How To Find When A Command Is Executed In Linux

By sk
Published: Updated: 716 views

This brief tutorial explains how to find when a command is executed in Linux and the benefits of knowing the execution time of a Linux command.

First, let us find out when a Linux command was executed.

Find When a Command is Executed in Linux

To find out when a command was executed in Linux, you can use the history command which shows a list of commands that have been executed, along with their date and time. Here’s how you can use it:

$ HISTTIMEFORMAT="%d/%m/%y %T " history

This will format the history output to show the date and time for each command. The %d/%m/%y format represents the day, month, and year, while %T represents the time in 24-hour format.

Sample Output for the above command would be:

[...]
1362  22/03/24 17:17:20 time sleep 2
1363  22/03/24 17:33:35 HISTTIMEFORMAT="%d/%m/%y %T " history

As you observed in the output, I have enabled timestamp in my bash history command output. As per the above output, I executed the time command at 5:17:20 PM on March 22, 2024.

Alternatively, you can use %F instead of %d/%m/%y. Both are correct.

$ HISTTIMEFORMAT="%F %T " history

If you're on Fish shell, use the following command to enable timestamp in history command output:

history --show-time='%F %T '

On Zsh shell, the command would be:

history -f

For more details on how to enable timestamp in Bash, Fish and Zsh shells' history command's output, please refer the following guides:

Determine Execution Time of a Specific Command

As you may noticed, the above command displayed the execution time of all the previously executed commands. What if you want to know the execution of a specific command? That's easy!

If you want to know the execution time of a certain command, you can use the time command. For example:

$ time your_command_here

This will measure how long it takes to execute and display the execution time after the command has completed.

Example:

$ time ls

The output will show three times:

  • real (total elapsed time),
  • user (time spent in user mode),
  • and sys (time spent in kernel mode).

Sample Output:

[...]
real	0m0.001s
user	0m0.001s
sys	0m0.000s

For more details about time command usage, I recommend you to refer the following guide:

Benefits of Knowing the Command Execution Time

Whether you’re a system administrator or a programmer, understanding how long a command takes to execute can be useful for optimizing performance.

Knowing the execution time of a command in Linux can be beneficial for several reasons:

  1. Performance Measurement: It helps you measure the performance of scripts or commands.
  2. Optimization: By identifying how long a command takes to execute, you can optimize your scripts to run more efficiently, reducing the overall execution time.
  3. Resource Usage: The time command can also display the system resource usage of the process, which is helpful for reviewing the efficiency of a specific command.
  4. Bottleneck Identification: If a particular task is taking longer than expected, it can help pinpoint performance bottlenecks within the system or script.
  5. System Monitoring: For system administrators, knowing the execution time is crucial for system monitoring and can aid in capacity planning and troubleshooting.

Conclusion

And, that's all for now. In this short tutorial, we discussed an useful Linux tip that will quickly help an user to find when a command was executed. Hope this helps.


Related Read:


You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More