How to check if a process has been finished?

Active3 hr before
Viewed126 times

7 Answers


But after some reading I think it's wrong since I am asking if waitpid finished successfully or not and not asking if the process was finished or not., @EmanuelP that's what I am asking help with, why should I save the result in a variable? the result means if waitpid worked or not, not if the process finished or still running – user15687371 Apr 19 at 9:30 ,-1: An error. errno should be checked. It can be EINTR, in which case you should just try calling waitpid again. It can also be ECHILD, in which case you either already established the process had ended earlier, or it is an invalid/unrelated pid. If this happens, something is wrong in your program's logic., The oddities of this function is a good reason to wrap it in a function that you reuse for your projects. Having a good read of the man page and writing this right once will save you a lot of headaches. – Emanuel P Apr 19 at 5:56

Thus, without caring for the process exit status, you can check it like this:

bool isdone(pid_t pid) {
      int status;
      while ((pid_t r = waitpid(pid, & status, WNOHANG) != pid) {
            if (r == 0) return false;
            if (errno == EINTR) continue;
            // handle errors
         return true;

I don't think it is possible to obtain such information after the process has already finished.,If you know beforehand that you will need that information, you can run the command as,However, after the process ends I want to find out how long it actually ran for but I can't seem to find that information.,Connect and share knowledge within a single location that is structured and easy to search.

If you know beforehand that you will need that information, you can run the command as

time <command>


~ > time sleep 1

real 0 m1 .003 s
user 0 m0 .002 s
sys 0 m0 .001 s
load more v

As has been stated in related StackOverflow question in wlk's answer, start time can be obtained via -o flag to ps and ltime format specifier:,Key point that should be understood about processes is that their metadata is not logged by default. You can, however, obtain certain information while process runs., Help Center Detailed answers to any questions you might have ,Stack Overflow en español

As has been stated in related StackOverflow question in wlk's answer, start time can be obtained via -o flag to ps and ltime format specifier:

ps - eo pid, lstart, cmd

Stop time, however, is not tracked by the system usually, so that's something you'll have to track yourself. Knowing PID of a process, you could poll for that PID existence, and echo the timestamp when it disappears

while kill - s 0 $pid;
do :;
sleep 0.25;
done && date
load more v

You can find that information in the system log /var/log/syslog and /var/log/messages,Depending on what process they were you may be able to find some info about their start time etc.,Is there a way to see processed that finished running or were killed a given amount of time ago?,If you are investigating some resource usage you can log it using pidstat and write it to a log file.


Feb 1 12:31:21 centos7 NetworkManager[809]: <info> dhclient started with pid 1319

Any time the system is running, processes are also running. You can use the ps command to find out which processes are running and display information about those processes.,The ps command has several flags that enable you to specify which processes to list and what information to display about each process.,In the previous example, the process ID for the ps -ef command is 19402. Its parent process ID is 16260, the /bin/ksh command.,The columns in the previous output are defined as follows:

ps - ef
load more v

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. ,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. ,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:

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
load more v

Fig.01: Linux check how long a openvpn process has been running on a server,In this tutorial, you learned steps to identify process IDs (PID) in Linux systems and then determine how long they have been running on your machine or cloud server. See ps command man page by typing the following ps command or read it online here: man ps,How to set up Redis sentinel cluster on Ubuntu or Debian Linux,Linux find how long a process running with ps:

  6176 / usr / sbin / openvpn--daemon 15: 25 65534 65534
load more v

Other "finished-process" queries related to "How to check if a process has been finished?"