触发服务从服务本身重新启动

我开发了一个应用程序mysvc ,它通过/etc/init.d/mysvc-service作为(Peta)Linux服务运行(名称应该不同,因为它们在petalinux / yocto词汇表中是不同的“应用程序”)。

/usr/bin/mysvc通过start-stop-daemon

 # start() start-stop-daemon -S -o --background -x /usr/bin/mysvc # stop() start-stop-daemon -K -x /usr/bin/mysvc 

它嵌入了一个简单的HTTP服务器,允许盒重启/关闭(工作),我想添加一个只运行/etc/init.d/mysvc-service restartRestart按钮(从命令行运行正常)。

当我想从程序本身使用Linux /etc/init.d/系统(构建命令行参数等)时,我检查了另一个重新运行程序本身的问题 (即响应HTTP请求)由我的服务器处理)所以我尝试了以下方法:

daemon()

将调用daemon()基本上fork()exit()父进程。 子进程实际上只是运行/etc/init.d/mysvc-service start

  if (daemon(1,1) == 0) { // Forks and exit() the parent. We are the child system("/etc/init.d/mysvc-service start"); // "start" and not "restart" because the parent process is not running anymore exit(0); } else { perror("daemon()"); } 

fork()

fork()并优雅地退出父级,而子级将运行/etc/init.d/mysvc-service start

  switch (fork()) { case 0: // Child runs command and exits system("/etc/init.d/mysvc-service start"); exit(0); case -1: // Error perror("fork()"); break; default: // Parent process: gracefully quit run = false; break; } 

两者都失败并具有相同的症状(因为它们本质上是等效的):父进程退出(如预期的那样)但是/etc/init.d system()调用没有生成新的mysvc

我可以在mysvc-service脚本中用简单的方法解决它while ! /usr/bin/mysvc ... ; do echo "Restart" ; done while ! /usr/bin/mysvc ... ; do echo "Restart" ; done while ! /usr/bin/mysvc ... ; do echo "Restart" ; done但我想知道是否可以在C中处理它(程序已经处理信号退出,重新加载配置等)。