Reputation: 343
I write simple C code
#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
[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
after this i run command
systemctl daemon-reload
systemctl enable Daemon1.service
systemctl start Daemon1.service
Then i got error
Job for Daemon1.service failed because the control process exited with error code.
See "systemctl status Daemon1.service" and "journalctl-xe" for details.
Then I check status of service with this command
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
Upvotes: 3
Views: 1004
Reputation: 1479
There is line in your output that is giving you a not so subtle hint: (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.
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.
Upvotes: 4