C++ deamon creation

I have a program that I want run as a deamon.  I believe I would use the fork() function, but I have no idea as to how I would do this.
mrcool4444Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
do u have access to advance unix programming by stevens? . ..
there is a good example in that book that shows how to make a process 'die'  and live like a  'daemon'.

its a simple C function that u can call even from CPP code and make  ur any program as a daemon..

in short .. fork the process.. and kill the parent process..
i'll get u the code when i get back to home..

0
akshayxxCommented:
http://www.yendor.com/programming/unix/apue/lib.svr4/daemoninit.c

this is the code that i was talking about ..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akshayxxCommented:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

AnhLePhuocCommented:
You don't say much about what kind of daemon you want to write. Assuming it is a system daemon you want to create then a simple and quick way is to try:
  /sbin/start-stop-daemon  --exec <your_program>
if your linux distribution provides it.
That can be used to start binary or shell scripts as daemons.
Check tha man page or run
  start-stop-daemon --help
for more details


However, if you just want to run something in the background, as the normal user, to do a job continuously then try something along the line:
  nohup <your_program> </dev/null >/dev/null 2>&1 &
0
mrcool4444Author Commented:
Well, I'll make it a little more detailed what I want.  I want an inet.d file in which i can start and stop the service, such as proftpd or apache.

So, in a file /etc/init.d/band:
start() {
        ebegin "Starting bandwith"
        start-stop-daemon --start --quiet --exec /etc/init.d/bandwith -- -d -p -d -p /var/run/bandwith.pid
        eend $?
}

With this, I can then stop the process by using a stop() function.

My problem though is I cannot get my program (bandwith) to run as a deamon.  When I execute /etc/init.d/band start, it starts bandwith but it doesn't deamonize it.

I need to beable to get bandwith deamonized and then have it return the daemon'ed process pid so init.d can stop it.
0
akshayxxCommented:
which OS are you on UNIX/LINUX ( version/distribution?)
0
mrcool4444Author Commented:
Gentoo linux
0
AnhLePhuocCommented:
Hi mrc00444,
What you you mean by saying your program doens't get daemonized. Does it hang (not returning), or does it die? Remember that stdin, stdout, and stderr will not be available to your program. If you to output something then you have to open the file explicitly.

Assuming that you 'daemon' is just a simple and single process (ie no forking) that loops infinitely then:
  start-stop-daemon -m -p /var/run/bandwidth_pid --start --background --exec /whatever/bandwidth
should start it.

There are a few problem in your example:
1) The --background or -b option is needed if your program doesn't fork() and exits.

2) You have the empty -- option in the wrong place. That would pass the options to your process and not the start-stop-daemon program as required.

3) The -m option is needed if you need a pid file to be created, as well as the -p option to specify the name of the PID file.

4) /etc/init.d/ directory is not a good place to store you daemon executable/binary. It's for the start/stop script only. But that not your problem here.

Good luck
0
AnhLePhuocCommented:
And to stop it:
  start-stop-daemon -p /var/run/bandwidth_pid --stop
0
dnatarajCommented:

Hey,
 The following will work as a Deamon perfectly:
Have a Nice Time.
 Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

int main(void)
{
          uid=getuid();
     if(getuid()!=0)
     {
          printf("Error : User is Not in root");
          exit(0);
     }
     pid=fork();
     if(pid>0)
     {
          printf("I'am the parent,child has pid %d",pid);
          exit(0);
     }
     else if(pid<0)
     {
          printf("Fork returned Error, Child is not created \n");
          exit(0);
     }
     else
     {
          printf("\n Child Process \n");
          setsid();
          umask(0);
          DaemonCode();
     }    
     
}

void DaemonCode()
{
     setuid(0);
     while(1)
     {
          usleep(10);
     }
     
}
0
dnatarajCommented:
Hi,
 I had posted the answer for the question, I request to accpet the answer or if some thing is wrong you can make comments.so that the points can be awarde.
Thanks,
Nataraj.
0
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: akshayxx {http:#8223489} & AnhLePhuoc {http:#8235475}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.