I have a daemon in init.d which except for the name and description has exactly the same structure as the standard ubuntu skeleton file. When I try to run said daemon using
sudo /etc/init.d/mydaemon start I get an error that starting the daemon failed with the message
Control process exited, code=exited, status=1/FAILURE which isn't very helpful as code 1 doesn't really mean anything as far as I can tell. While debugging this, at one point I decided to change the verbose variable at /lib/init/vars.sh from no to yes, just to provoke some output and upon doing that, the daemon runs flawlessly. Yet when I change verbose back to no, I get the same errors as previously. Have any of you ever encountered somehthing like this and now what may be causing it?
Also, the daemon code is in c++ and is the following (although i don't think it's neccesarily relevant for this):
#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <syslog.h> #include <string.h> #include <string> using namespace std; #define DAEMON_NAME "mydaemon" void process(){ syslog (LOG_NOTICE, "Writing to log from Daemon"); } int main(int argc, char *argv[]) { //Set our Logging Mask and open the Log setlogmask(LOG_UPTO(LOG_NOTICE)); openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); pid_t pid, sid; //Fork the Parent Process pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } //We got a good pid, Close the Parent Process if (pid > 0) { exit(EXIT_SUCCESS); } //Change File Mask umask(0); //Create a new Signature Id for our child sid = setsid(); if (sid < 0) { exit(EXIT_FAILURE); } // Change to root chdir("/"); //Close File Descriptors int x; for (x = sysconf(_SC_OPEN_MAX); x>=0; x--) { close (x); } //---------------- //Main Process //---------------- while(true){ process(); //Run our Process sleep(30); //Sleep for 30 seconds break; } //Close the log closelog (); return 0; }
forkandsetsidboilerplate code is necessary if you configure your program to run as a systemd service (type=simple).