Skip to content
Advertisement

How to check if process is running on Red Hat Linux?

I’ve been using a modified class I found to check if another instance of the same process is already running, the problem is that the method of checking for the process adds another instance of the same process.

When my application starts, a new process ID is created and is visible with:

JavaScript

With this I get a since entry returned, I then check for another instance of the application using:

JavaScript

As soon as the ‘start’ method is called another instance of the same application appears in the process table, again, verified with:

JavaScript

Two entries now appear each with a different PID. I’ve also tried:

JavaScript

The result is the same, two entries in process table.

Is there a way to check the process table for another instance without adding an additional instance?

Advertisement

Answer

I recommend using a pid file. It’s the standard procedure for daemons on linux.

JavaScript

Source The Linux Programming Interface

This creates a file and locks it so no other process can open it. It marks it as close on exit so the file will close when the process exits through normal or abnormal termination. If an instance of your program is already running, this function will fail and you can just exit.

EDIT: The pid file should be in the temporary partition. the specific location varies depending on your distribution, just look on your system where other daemons create their pid files.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement