having server start|stop|restart mechanisms

hi, I'm writing a server program but wish to have something like the following:

./server start
./server stop
or
./server restart

to manipulate the instance(process) of that program, how do I go about implementing it?

aka something akin to apachectl start, apachectl stop or apachectl restart, etc so that I can just call that in my bash shell script in /etc/init.d/
Last edited on
Generally your shell script should issue a SIGTERM to the server process to shut it down.

Then
start = launch the server process;
stop = send SIGTERM to the process and let it handle it;
restart = stop followed by start

ok I have 3 questions:
Q1) so that means, the shell script will have to issue the kill <pid> command to kill my server process?
Q2) and my server would have to catch the SIGTERM signal and perform any cleanups or complete any outstanding transactions before exiting?
Q3) How do I check within my program if another instance of it is already running?
Last edited on
Yes and yes.

As for third question, a variety of ways. You can create a "lock" file in /var somewhere (as a temporary file). Existence of the file means the server is already running.
You don't say which operating system this is for. The files in init.d are all pretty similar. This article gives a good overview of initscripts on Linux. Towards the bottom of the article you will find a tutorial for writing an init script.

http://www.yolinux.com/TUTORIALS/LinuxTutorialInitProcess.html

The init script functions library, which provides a number of useful init script functions, is mentioned briefly in that tutorial and documented in more detail here:

http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html

what if in my server program I have something like this:

1
2
3
4
5
6
7
8

while ( !exitFlag )
{
    ....
    remoteClientFD = accept( ... );
    ....
}


and when the signal handler receives and sets the exitFlag to true, accept is in the blocking state, how do I make accept return and exit out of the loop?
It should by default do that if you register a signal handler and catch a signal.
It should by default do that if you register a signal handler and catch a signal.

even if the server is multithreaded?
Yes, though threads and signals are a bit tricky with multithreaded apps, and it depends on what thread library you're using

I'm using pthreads...
You will want your main program to register a signal handler for SIGTERM and then you'll want all of your threads to block SIGTERM. This will cause the signal to be received by the main program, which can then proceed to do pthread_cancel()s on all the threads and perform any other cleanup necessary.
Using OS provided signals works but an alternative solution is for you to build your own customized signal interrupt concept. Could be a TCP socket waiting for some client request (most likely coming from an Administrator client program) that send a SHUTDOWN, RESTART, START etc etc whatever commands. Of cuz since signals is OS level, you also need to build your own interrupt routines so most developers I have seen go straight to the OS signal approach. Effective, less time consuming and it works.

PS I also read elsewhere for some Unixes, the signal timing is not very reliable but I would presume those problems exist only for old Unix version. Newer Unix versions like Linux should not suffer from this syndrome.
Topic archived. No new replies allowed.