Tuesday, October 15, 2024

Linux Command to Find how Long a process is Running in UNIX - Example

Hello guys, today, I am going to share one of the useful tips to find out how long a particular process is running in Linux. This is very useful while you are troubleshooting an issue and want to know whether your process or service is restarted fine on a daily or weekly basis. So you checked your process is running in Linux operating system and it's running fine, by using ps command. But now you want to know, from how long process is running, what is the start date of that process etc. Unfortunately, PS command in Linux or any UNIX based operating system doesn't provide that information.

But as said, UNIX or Linux has commands for almost everything and if there is no command, you can also check some important files in the /etc directory to find out some useful info. 

It’s been a long time, I have posted any UNIX or Linux command tutorial, after sharing some UNIX productivity tips. Hence, I am going to share this nice little tip, which you can use to check how long a particular process is running.

So I thought to share this nice little tip about finding runtime of a process in UNIX based systems like Linux and Solaris.  In this UNIX command tutorial, we will see step by step guide to finding, since when a particular process is running in a server.

Linux Command to Find how Long a process is Running in UNIX - Example






Linux Commands to find Runtime of a Process

UNIX command to find runtime of a process in LinuxAs 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 ps command like

$ ps -ef | grep java
user 22031 22029   0   Jan 29 ?          24:53 java -Xms512M -Xmx512 Server

here 22031 is process id or PID. By the way, if there are more than one Java process running in your server than you might want to use a more specific grep command to find PID. 


Step 2: Find the Runtime or start time of a process
Once you found PID, you can look into proc directory for that process and check creation date, that's the time when your process was started. 

By looking that timestamp you can easily find from how long your process is running in Linux. 

In order to check the timestamp of any process id procs directory, you can use following ls UNIX command with option -ld as shown below :

$ ls -ld /proc/22031
dr-x--x--x   5 user     group           832 Jan 22 13:09 /proc/22031

Here process with PID 22031 has been running from Jan 22, 13:09.

If you are lucky, sometimes PS command also shows when a particular program has been started as shown in the following image:

How to Find Runtime of a Process in Linux and UNIX


And, if you need an alternative, you can also use

$ ps -eo pid,comm,etime | grep process_name

Where
ps: Command to report process status
-e: Select all processes
-o: Specify output format
pid: Process ID
comm: Command name
etime: Elapsed time since process start
| grep process_name: Filter for specific process


That's all on How to find uptime for a process in Java. Yes, it's similar to what uptime command return for a server, but you can use this to find out how long a process has been running or when exactly it was started or rebooted.

I really like these UNIX tips, as it's a great tool to find out how long a process is running in UNIX or Linux and I have used a couple of time to check if our web server is restarted or not. 


Related UNIX Command Tutorials
  • 10 examples of find command in UNIX (examples)
  • How to send mail with attachments from Linux? (mailx)
  • 10 examples of grep command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from a hostname and vice-versa in Linux (command)
  • 10 examples of xargs command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 tips to work fast in UNIX? (tips)
  • Linux find + du + grep example (example)
  • 10 Examples of curl command in Linux (cURL)
  • 10 Examples of chmod command in Linux (chmod)
  • A Practical Guide to Linux Commands, Editors, and Shell Programming (guide)
    Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

    P. S. - If you are looking for some free online courses to start your Linux journey, you should check out my list of Free Linux Courses for Programmers, IT Professionals, and System Administrators.


    1 comment :

    J. Ernesto Aneiros said...

    Uhmm, what about the ps -o option with something like etime?

    My 20 cents tip: man ps or info ps.

    Have a nice week you all.

    Post a Comment