?
Solved

C++ deamon creation

Posted on 2003-03-28
13
Medium Priority
?
776 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:mrcool4444
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8223480
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
 
LVL 8

Accepted Solution

by:
akshayxx earned 252 total points
ID: 8223489
http://www.yendor.com/programming/unix/apue/lib.svr4/daemoninit.c

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

Expert Comment

by:akshayxx
ID: 8223505
0
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 
LVL 1

Assisted Solution

by:AnhLePhuoc
AnhLePhuoc earned 248 total points
ID: 8235475
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
 

Author Comment

by:mrcool4444
ID: 8236792
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8236883
which OS are you on UNIX/LINUX ( version/distribution?)
0
 

Author Comment

by:mrcool4444
ID: 8240653
Gentoo linux
0
 
LVL 1

Expert Comment

by:AnhLePhuoc
ID: 8260346
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
 
LVL 1

Expert Comment

by:AnhLePhuoc
ID: 8260357
And to stop it:
  start-stop-daemon -p /var/run/bandwidth_pid --stop
0
 
LVL 1

Expert Comment

by:dnataraj
ID: 8448873

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
 
LVL 1

Expert Comment

by:dnataraj
ID: 9342258
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10225196
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

Featured Post

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question