Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C++ deamon creation

Posted on 2003-03-28
13
Medium Priority
?
783 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
  • 4
  • 3
  • 2
  • +2
12 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

577 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