- Is there a way to find the running time of the last executed command in the shell?
- 7 Answers 7
- 3 Ways to check process running time in Linux
- Check process running time with ps command in Linux
- Check process CPU time using top command in Linux
- Check process running time using proc filesystem in Linux
- How to Find Uptime of Particular Linux Process
- How to Find Uptime of Your Linux System
- 1. Check Linux Running Time Using Uptime Command
- 2. Check Linux Uptime Using W Command
- 3. Check Linux Uptime Using Top Command
- How to Display Running Processes in Linux
- 1. List Running Linux Processes Using top Command
- 2. Show Linux Processes Using ps Command
- 3. View Linux Processes Using htop and atop Commands
- How to Find Individual Process Uptime in Linux
- 1. Find Particular Process Uptime Using Ps Command
- 2. Find Specific Process Uptime Using Monit
Is there a way to find the running time of the last executed command in the shell?
Is there a command like time that can display the running time details of the last or past executed commands on the shell?
7 Answers 7
zsh has some built in features to time how long commands take.
If you enable the inc_append_history_time option with
Then the time taken to run every command is saved in your history and then can be viewed with history -D .
I do not know, how it is in bash, but in zsh you can define preexec and precmd functions so that they save the current time to variables $STARTTIME (preexec) and $ENDTIME (precmd) so you will be able to find the approximate running time. Or you can define an accept-line function so that it will prepend time before each command.
UPDATE: This is the code, which will store elapsed times in the $_elapsed array:
preexec () < (( $#_elapsed >1000 )) && set -A _elapsed $_elapsed[-1000,-1] typeset -ig _start=SECONDS > precmd()
Then if you run sleep 10s :
% set -A _elapsed # Clear the times % sleep 10s % sleep 2s ; echo $_elapsed[-1] 10 % echo $_elapsed 0 10 0
No need in four variables. No problems with names or additional delays. Just note that $_elapsed array may grow very big, so you need to delete the first items (this is done with the following piece of code: (( $#_elapsed > 1000 )) && set -A _elapsed $_elapsed[-1000,-1] ).
UPDATE2: Found the script to support zsh-style precmd and preexec in bash. Maybe you will need to remove typeset -ig (I used just to force $_start to be integer) and replace set -A var . with var=( . ) in order to get this working. And I do not know how to slice arrays and get their length in bash.
UPDATE3: Found one problem: if you hit return with an empty line preexec does not run, while precmd does, so you will get meaningless values in $_elapsed array. In order to fix this replace the precmd function with the following code:
precmd () < (( _start >= 0 )) && set -A _elapsed $_elapsed $(( SECONDS-_start )) _start=-1 >
3 Ways to check process running time in Linux
In this blog post, we will discuss three different ways to check the running time of a process in Linux. This is a useful tool for troubleshooting and determining how long specific processes are taking to run. We will also discuss some of the benefits of each method. Let’s get started!
Check process running time with ps command in Linux
The best way to check process running time in Linux is using ps command. Open the terminal and type ps -p pid -o etime,etimes. It will list the process running time.
The ps command can be used to view information about all running processes. To use this command, simply type “ps aux” into your terminal. You will then see a list of all running processes, as well as their PID (process ID), and the amount of time they have been running.
- etime Display elapsed time since the process was started, in the form [[DD-]hh:]mm:ss.
- etimes Display elapsed time since the process was started, in seconds.
You need to pass the -o etimes or -o etime to the ps command. The syntax is:
let us find and print the PID creation date. In other words find out when the process was started on Linux, enter:
- $ sudo ps -p -o start,etimes,etime
- $ sudo ps -p -o start,etimes,etime
- $ sudo ps -p -o pid,cmd,start,etimes,etime
- $ sudo ps -C -o pid,cmd,start,etimes,etime
Check process CPU time using top command in Linux
The second method is to use the “top” command. This command provides a real-time view of all running processes. It also displays a variety of other information about each process, such as CPU usage and memory usage. To use the top command, simply type “top” into your terminal. You will then see a list of all running processes, sorted by CPU usage.
Top command shows you the total CPU time the task has used since it started. But it doesn’t include the elapsed time. So, don’t get confused between top and ps commands.
Check process running time using proc filesystem in Linux
However, the above command doesn’t show you the exact start time of the process and use the following format to check that. The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information.
It’s sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but run time system information (e.g. system memory, devices mounted, hardware configuration, etc).
# ls -ld /proc/16337
dr-xr-xr-x. 9 root root 0 Aug 5 17:20 /proc/16337/
Both of these methods have their own benefits. The “ps” command is great for getting a quick overview of all running processes. The “top” command provides real-time information about each process, but it can be a bit overwhelming to look at.
Whichever method you choose, checking the running time of processes can be a valuable tool for troubleshooting and performance analysis. Try out both methods and see which one works best for you!
We hope this blog post has been helpful. If you have any questions or comments, please feel free to reach out to us! We would be happy to help. Thanks for reading!
How to Find Uptime of Particular Linux Process
Along with the system uptime, it’s also possible in the Linux operating system to check the uptime of a particular Linux process or service. Various Linux tools are available that can be used to quickly know how long an individual process has been running in Linux.
In this article, I’ll help you to know not just the various methods to find the uptime of a Linux system but also list all running processes and uptime of a particular Linux process, which you can apply on all Linux-based operating systems.
How to Find Uptime of Your Linux System
There are three most basic commands available that you can refer to check the time of your last system reboot: uptime, w, and top.
1. Check Linux Running Time Using Uptime Command
By just typing ‘uptime‘ in the terminal, it will display a result in one line.
Here, as you move from left to right, the line shows the current time, the number of hours and minutes your Linux system has been running, the number of current users logged in, and the load average of a system for the past 1, 5, and 15 minutes.
If you want results in another format with particular information, you can use various options available with the uptime command. For instance, using the -p option displays only the time up to which the Linux system is up and running.
2. Check Linux Uptime Using W Command
Similar to uptime, the w command also displays the same uptime pieces of information along with users logged on and what they are doing.
3. Check Linux Uptime Using Top Command
Being one of the most popular Linux tools for process management, the top command also shows how long you have been logged on to the system.
Furthermore, it goes beyond presenting a table containing a list of all running services on a system with their various information like CPU and memory usage.
How to Display Running Processes in Linux
If you don’t know there are no particular Linux tools that can directly give you the uptime of a particular service. You first need a command that can list currently running processes and combine that with another command (usually grep command) to identify the individual processes and their uptime.
That’s why before we head toward finding the uptime of a particular Linux service, let’s also see how we can list running processes in Linux using various methods.
1. List Running Linux Processes Using top Command
As you saw above, along with the uptime of the Linux system, the top command also displays the list of running processes in order of decreasing CPU usage by just executing:
Each column of the output in a tabular form lists particular information like the first column PID displays process IDs of services.
2. Show Linux Processes Using ps Command
The ps (processes status) command also lists currently active running processes. However, unlike the top, it presents only a snapshot of a process as a static list instead of real-time data.
Using various options along with the ps command will give detailed information. For instance, running the below command displays a list of all processes on the system with process ids, CPU, and memory usage.
3. View Linux Processes Using htop and atop Commands
Apart from the pre-installed top and ps commands, you can also employ other tools like htop and atop that offers the same output but in an easy-to-understand and user-friendly way.
To install htop and atop on your Linux system, use the respective package managers as shown.
Install Htop in Linux
$ sudo apt install htop [On Debian, Ubuntu and Mint] $ sudo yum install htop [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a htop [On Gentoo Linux] $ sudo apk add htop [On Alpine Linux] $ sudo pacman -S htop [On Arch Linux] $ sudo zypper install htop [On OpenSUSE]
Install atop in Linux
$ sudo apt install atop [On Debian, Ubuntu and Mint] $ sudo yum install atop [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a atop [On Gentoo Linux] $ sudo apk add atop [On Alpine Linux] $ sudo pacman -S atop [On Arch Linux] $ sudo zypper install atop [On OpenSUSE]
Now, just type the htop or atop commands and you’ll see a piece of real-time detailed information.
If you want to explore more about the commands, check out its man pages by running the command:
How to Find Individual Process Uptime in Linux
Till now, you learned how to get system uptime and a list of all currently running processes. It’s time to get the uptime of a particular running process.
1. Find Particular Process Uptime Using Ps Command
As you saw above, ps provides a snapshot of active processes on the system, we will pipe this output to identify particular processes using a grep (global regular expression print) utility and add some options that will give the desired output.
To do so, execute the below command in your Linux terminal:
$ ps -eo pid,lstart,,etime,cmd | grep -i bash OR $ ps -eo pid,lstart,,etimes,cmd | grep -i bash
Here, etime is the elapsed time since service was started, in the form of [[DD-]hh:]mm:ss and etimes also the same with a difference in results in seconds. If you find it difficult to run the above command, you can make use of an alias to shorten it for regular use.
Instead of the application name, you can also give the process id that you can get from the output list of active processes.
2. Find Specific Process Uptime Using Monit
Though you may first need to install it, Monit is yet another small utility for managing and monitoring processes, files, and filesystems on a Unix system along with automatic maintenance and repair.
Install Monit on Linux
$ sudo apt install monit [On Debian, Ubuntu and Mint] $ sudo yum install monit [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a monit [On Gentoo Linux] $ sudo apk add monit [On Alpine Linux] $ sudo pacman -S monit [On Arch Linux] $ sudo zypper install monit [On OpenSUSE]
Once installed, you also need to configure Monit in Linux before you start using it for process uptime. First, launch the monit daemon and check the status by running:
$ sudo monit $ sudo systemctl status monit
Surprisingly, Monit also provides a web interface for those who prefer a graphical interface instead of a command line. So, to enable the web interface, you need to edit the monit configuration file located mainly at /etc/monit/monitrc for Debian-based Ubuntu and Linux Mint and at /etc/monit.conf for Red-Hat-based Fedora.
Just open the file in your favorite editor:
And uncomment the following lines shown below screenshot i.e. IP address and monit admin user.
Next, you also need to restart the monit service to apply your new config changes. Enable it also if you want to auto-run during system startup.
$ systemctl start monit $ systemctl enable monit $ systemctl status monit
Now, you’re ready to access the monit web interface by navigating to the localhost URL in your browser.
As you will hit the url, it will prompt you for credentials. You can use the default “admin” as username and “monit” as password. Once entered, you’ll see the below screen.
The interface will list all running processes along with other details like CPU, memory usage, boot time, and uptime.
At last, we learned to check the uptime of individual processes in Linux, but before we also need its process ID which we saw how we can get using various Linux command line tools. You can explore more above all methods to get the best-desired output in your comfortable way.