gpt4 book ai didi

c - 如何使用简单的 Daemon C 代码在 Ubuntu 16.10 中启动 systemctl 服务

转载 作者:行者123 更新时间:2023-12-03 09:49:07 24 4
gpt4 key购买 nike

我写简单的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;
}

和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

并存储在位置

/etc/systemd/system

在此之后我运行命令

systemctl daemon-reload

systemctl enable Daemon1.service

systemctl start Daemon1.service

然后我得到了错误

Job for Daemon1.service failed because the control process exited with error code.

See "systemctl status Daemon1.service" and "journalctl-xe" for details.

然后我用这个命令检查服务状态

systemctl status Daemon1.service

然后我明白了

● 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'.

我的服务没有运行。我需要更改什么才能使我的代码正常工作。我将 C 代码的可执行文件提供给 Daemon1.service

中的 ExecStart=

最佳答案

您的输出中有一行给您的提示并不那么微妙:(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)

修改您的代码,在 fork 后向操作系统返回 0 而不是 1。

        if(pid>0){

exit(0);
}

在那次小调整之后,您应该重新开始工作以继续前进:

# 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.

关于c - 如何使用简单的 Daemon C 代码在 Ubuntu 16.10 中启动 systemctl 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58934909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com