top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to start an application as daemon?

+1 vote
389 views

I have an application and whenever i start it i want it to be started as daemon process.
How will i achieve that?

Below is the sample code:

#include<stdio.h>
main()
{
    while(1)
    {
             printf(" Do Some Task Here\n");
     }
}
posted Nov 30, 2015 by Chirag Gangdev

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
If you want to de-associate the script from the terminal and need to keep it running even after the terminal close then run it as nohup a.out &
If you want to make it part of startup of linux then invoke it from the script /etc/rc.local

Comment you needs and I will provide detail descriptive answer.
Yes thanks, i was aware about nohup a.out &... i want something like that only but i am not suppose to start an application by nohup a.out &, it should be something inside the program.
Is it possible?
Try something like
a.out &>/dev/null &

let me know if it satisfies your need.
Thanks Sir,
But what i want is something inside a program so whenever anyone runs that, it will be as a daemon only.
Is it possible?
I searched and somehow i got <a href="http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html">this</a> but i don't know how to use setsd() function.

is there anyother way?

1 Answer

+1 vote
 
Best answer

Now I got the problem -

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
  FILE *fp= NULL;
  pid_t process_id = 0;
  pid_t sid = 0;
  // Create child process
  process_id = fork();

  // Indication of fork() failure
  if (process_id < 0)
  { 
    printf("fork failed!\n");
    // Return failure in exit status
    exit(1);
  }

  // PARENT PROCESS. Need to kill it.
  if (process_id > 0)
  {
    printf("process_id of child process %d \n", process_id);
    // return success in exit status
    exit(0);
   }

  //unmask the file mode
  umask(0);

  //set new session
  sid = setsid();
  if(sid < 0)
  {
    // Return failure
    exit(1);
  }

  while (1)
  {
      sleep(1);
      printf(" Do Some Task Here\n");
  }
  return (0);
}

Logic
Make child process orphan and is taken over by the init process.
Call setsid() function to run the process in new session and have a new group. Now child is de-associated with terminal.

How to execute

$ gcc -Wall mydeamon.c -o mydeamon
$ sudo ./mydeamon

PS: Program is not tested so you may need to debug if needed.

answer Nov 30, 2015 by Salil Agrawal
Similar Questions
+3 votes

I have a script which will start few daemon processes,
What i want is, After reboot this script should start with an argument.

Can anyone help?

+2 votes

I am new to Linux and want whenever the machine restarts my program should be executed, how can I achieve that?

+3 votes

I am getting
/usr/share/sendmail/sendmail: 899: /usr/share/sendmail/sendmail: /usr/sbin/sendmail-msp: not found
this message in my email how to stop this.

0 votes
#include<stdio.h>
main()
{
        int a=10;
        printf("%d\n",a++==a);
}

Output should be 1 but it is coming as 0,
Can anyone please explain?

...