C – Query whether the service is running

Query whether the service is running… here is a solution to the problem.

Query whether the service is running

How do I query in C if the service (dnsmasq) is running?

Solution

According to dnsmasq man page, by default it writes pid files to /var/run/ dnsmasq.pid。 The file will be a text file containing an integer representing the process ID. Open the file, read the integer, and call kill(pid, 0) to see if the process is in an event state at that pid. (While there’s no guarantee that checking for the presence of a PID won’t reveal other processes running on that PID, it’s usually good enough.) )

Related Problems and Solutions