I wrote a simple server in C and would like to have the same functionality calling it as other C daemons (such as calling it with ./ftpd start and closing that instance with ./ftpd stop). Obviously the problem I’m having is that I do not know how to grab the current instance of the running program. I can parse the options just fine (using getopt / optarg) but at the moment, ./my-program stop just starts a new instance vs. calling ./my-program start which starts it up fine.
The reason I want to do this is because another program will be signaling my server to stop, so a call like ./my-program stop is very simple, which can then stop the server loops and close all the open fd’s. Thanks!
Is it possible to do this in the C program itself or is this usually handled externally?
You will always run a separate process when running it with ‘stop’ argument.
You need to find the running daemon and sending it a signal to stop it. When your deamon is started (option ‘start’), you might want to store its pid somewhere so that when it is started with ‘stop’ option, it can retrieve the pid of the daemon and send it a signal to stop it. You can also parse the process table to find your daemon, but that will be more complex.