libASSA Programmer's Manual | ||
---|---|---|
<<< Previous | Chapter 4. Class Reference | Next >>> |
PidFileLock is a rocess ID file encapsulation class. It is very typical for a UNIX daemon process to write its PID to a file and then lock the file. This prevents other copies of the same process from running on the same host, and helps administering and restarting daemon processes.
#include <assa/PidFileLock.h> class PidFileLock : public flock { public: PidFileLock (); ~PidFileLock (); bool lock (const std::string& filename_); int get_error () const; const char* get_error_msg () const; void dump (); }; |
Class GenServer uses PidFileLock by default (see Section 4.15 for command-line options and general behavior).
However, it is very simple to use PidFileLock on its own. For example:
#include <assa/PidFileLock.h> class MyServer { private: PidFileLock m_pidflock; public: void run (); }; |
void MyServer::run () { string proc_name = "MyServer"; if (! m_pidflock.lock (proc_name)) { log_error("Failed to lock PID file: %s", m_pidfile_lock.get_error_msg ()); exit (1); } // Enter MyServer's data processing section }; |
While MyServer is running, a PID file .MyServer.pid is created in a working directory. Its context holds MyServer's PID number written.
The destructor of PidFileLock takes care of unlocking and removing PID lock file from the file system.
Given PID file, it is extermely easy to write administrative shell scripts and cron jobs to control the lifespan of your process. For example, the following little shell scripts lets you to stop, start and monitor the running status of your process.
#!/usr/bin/ksh #------------------------------------------------------------------------------- # Name: run.sh # Desc: Start MY-SERVER daemon process #------------------------------------------------------------------------------- pidfile=$HOME/.MY-SERVER.pid if [[ -a "$pidfile" ]] then pid=`cat $pidfile` printf "\nMY-SERVER ($pid) is already running\n" else MY-SERVER --daemon --log-file=MY-SERVER.log fi |
#!/usr/bin/ksh #------------------------------------------------------------------------------- # Name: stop.sh # Desc: Terminate MY-SERVER daemon process if it is running #------------------------------------------------------------------------------- pidfile=$HOME/.MY-SERVER.pid if [[ -a "$pidfile" ]] then pid=`cat $pidfile` printf "Calling kill $pid" kill $pid printf "\nMY-SERVER has been terminated\n" else printf "\nMY-SERVER is not running\n" fi |
#!/usr/bin/ksh #------------------------------------------------------------------------------- # Name: stat.sh # Desc: Is MY-SERVER daemon process running? #------------------------------------------------------------------------------- pidfile=$HOME/.MY-SERVER.pid if [[ -a "$pidfile" ]] then pid=`cat $pidfile` printf "status: MY-SERVER ($pid) appears to be running\n" exit 0 else printf "status: MY-SERVER appears not running\n" exit 1 fi |
Now you can schedule a cron job to test the running status of your program using these scripts.
<<< Previous | Home | Next >>> |
Singleton Class | Up | TimeVal Class |