How to Find How Long a Process Has Run in UNIX
Here is an easy two-step process to identify process IDs in Unix systems and then determine how long they've been running for.
Join the DZone community and get the full member experience.
Join For FreeHello everyone! Today, I am going to share a useful tip to find out how long a particular process has been running. This is very useful when you are troubleshooting an issue and want to know whether your process or service restarted fine on a daily or weekly basis.
UNIX or Linux has commands for almost everything, and if there is no command, you can check some important files in the /etc directory to find out some useful info.
So I thought to share this nice little tip to find the runtime of a process in UNIX-based systems like Linux and Solaris.
Linux Commands to Find Process Runtimes
As I said, there is no single command, which can tell us that from how long a process is running. We need to combine multiple commands, and our knowledge of UNIX based systems to find uptime of a process.
Step 1: Find Process id by Using the ps Command
$ ps -ef | grep java
user 22031 22029 0 Jan 29 ? 24:53 java -Xms512M -Xmx512 Server
Here, 22031 is the process id. By the way, if there is more than one Java process running on your server, then you might want to use a more specific grep command to find the PID.
Step 2: Find the Runtime or Start Time of a Process
Once you have the PID, you can look into proc directory for that process and check the creation date, which is when the process was started.
By checking that timestamp, you can easily find from how long your process has been running. In order to check the timestamp of any process id in the procs directory, you can use following UNIX command with the option -ld as shown below:
$ ls -ld /proc/22031
dr-x--x--x 5 user group 832 Jan 22 13:09 /proc/22031
Here, the process with PID 22031 has been running from Jan 22, 13:09.
If you are lucky, sometimes the ps command also shows when a particular program was started, as shown in the following image:
Published at DZone with permission of Javin Paul, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments