diff --git a/robot.service b/robot.service new file mode 100644 index 0000000..d4ff7a9 --- /dev/null +++ b/robot.service @@ -0,0 +1,12 @@ +#! /bin/sh +[Unit] +Description=Robot daemon +[Service] +Type=forking +PIDFile=/var/run/robot.pid +WorkingDirectory=/home/root/Robotd +ExecStart=/etc/init.d/robot start +ExecReload=/etc/init.d/robot restart +ExecStop=/etc/init.d/robot stop +[Install] +WantedBy=multi-user.target diff --git a/robotd.c b/robotd.c index 73e5178..ec38ee9 100644 --- a/robotd.c +++ b/robotd.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,18 @@ int main(int argc, char ** argv) { return EXIT_FAILURE; } + // Create PID file + FILE * pid = fopen("/var/run/robot.pid", "w"); + + if (pid == NULL) { + perror("FILE * pid = fopen(\"/var/run/robot.pid\", \"w\")"); + syslog(LOG_DAEMON | LOG_ERR, "Failed to create PID file."); + return EXIT_FAILURE; + } + + fprintf(pid, "%d", getpid()); + fclose(pid); + // Open a syslog connection openlog(argv[0], LOG_NDELAY | LOG_PID, LOG_DAEMON); syslog(LOG_DAEMON | LOG_NOTICE, "Started robot daemon.");