简体   繁体   English

如何使用简单的守护程序 C 代码在 Ubuntu 16.10 中启动 systemctl 服务

[英]How to start systemctl service in Ubuntu 16.10 with simple Daemon C code

I write simple C code我写了简单的 C 代码

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/stat.h>
int main(){

        pid_t pid;
        pid=fork();

        if(pid>0){

                exit(1);
        }



        FILE *fp;
        fp=fopen("pid.pid","a");
        fprintf(fp,"%d",getpid());
        fclose(fp);
        printf("\npid = %d\n",pid);
        printf("\ngetpid = %d\n",getpid());
        puts("\nAfter fclose() \n");
        umask(0);

        while(1){}

return 0;
}

and Daemon1.service和 Daemon1.service

[Units]
Description=Socket programming with Daemon

[Service]
User=root
Type=forking
WorkingDirectory=/Omkar/Doc/systemctl/
ExecStart=/Omkar/Doc/systemctl/main
Restart=always
PIDFile=/Omkar/Doc/systemctl/pid.pid

[Install]
WantedBy=multi-user.target

and stored at location并存储在位置

/etc/systemd/system /etc/systemd/系统

after this i run command在此之后我运行命令

systemctl daemon-reload systemctl 守护进程重载

systemctl enable Daemon1.service systemctl enable Daemon1.service

systemctl start Daemon1.service systemctl 启动 Daemon1.service

Then i got error然后我得到了错误

Job for Daemon1.service failed because the control process exited with error code. Daemon1.service 的作业失败,因为控制进程以错误代码退出。

See "systemctl status Daemon1.service" and "journalctl-xe" for details.有关详细信息,请参阅“systemctl status Daemon1.service”和“journalctl-xe”。

Then I check status of service with this command然后我用这个命令检查服务状态

systemctl status Daemon1.service systemctl status Daemon1.service

then i got this然后我得到了这个

● Daemon1.service
   Loaded: loaded (/etc/systemd/system/Daemon1.service; enabled; vendor preset: enabled)
   Active: failed (Result: start-limit-hit) since Tue 2019-11-19 18:21:26 IST; 3min 26s ago
  Process: 5868 ExecStart=/Omkar/Doc/systemctl/main (code=exited, status=1/FAILURE)

Nov 19 18:21:26 pt32-H81M-S systemd[1]: Failed to start Daemon1.service.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Unit entered failed state.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Failed with result 'exit-code'.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Service hold-off time over, scheduling restart.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Stopped Daemon1.service.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Start request repeated too quickly.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Failed to start Daemon1.service.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Unit entered failed state.
Nov 19 18:21:26 pt32-H81M-S systemd[1]: Daemon1.service: Failed with result 'start-limit-hit'.

My service is not running.我的服务没有运行。 What should i need to change so my code will work.我应该改变什么,这样我的代码才能工作。 I give executable file of C code to ExecStart= in Daemon1.service我将 C 代码的可执行文件提供给 Daemon1.service 中的ExecStart =

There is line in your output that is giving you a not so subtle hint: (code=exited, status=1/FAILURE)您的 output 中有一条线给您一个不那么微妙的提示:(code=exited, status=1/FAILURE)

● Daemon1.service
   Loaded: loaded (/etc/systemd/system/Daemon1.service; enabled; vendor preset: enabled)
   Active: failed (Result: start-limit-hit) since Tue 2019-11-19 18:21:26 IST; 3min 26s ago
  Process: 5868 ExecStart=/Omkar/Doc/systemctl/main (code=exited, status=1/FAILURE)

Modify your code return 0 instead of 1 to the OS after forking.修改你的代码在分叉后返回 0 而不是 1 给操作系统。

        if(pid>0){

                exit(0);
        }

You should be back in business to move forward after that small adjustment:在进行了小幅调整后,您应该重新开始工作以继续前进:

# systemctl status Daemon1.service
● Daemon1.service
   Loaded: loaded (/etc/systemd/system/Daemon1.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-11-19 08:49:33 CST; 5s ago
  Process: 20484 ExecStart=/root/stackoverflow/Daemon1 (code=exited, status=0/SUCCESS)
 Main PID: 20486 (Daemon1)
   CGroup: /system.slice/Daemon1.service
           └─20486 /root/stackoverflow/Daemon1

Nov 19 08:49:33 lm systemd[1]: Starting Daemon1.service...
Nov 19 08:49:33 lm systemd[1]: Started Daemon1.service.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM