Link to home
Start Free TrialLog in
Avatar of kgreddy
kgreddy

asked on

How to run a program as daemon ?

I have an application which should run as daemon when user specifies -d option.
I am doing the following to make it daemon.

1)Fork
2)If successful, exit parent
3)make child as session leader
4)cd /
5)umask(0)

My application runs fine when it is not running as daemon(when -d optionnot specified)
When I specify -d option, when I debug, I can see that parent exits. then I cant track what happens.

Please help
kgreddy
Avatar of ahoffmann
ahoffmann
Flag of Germany image

you need a debugger which follows the forked process, or use a debugger which can attach to a running process
See man gdb for example.
Avatar of Nisus091197
Nisus091197

Under Solaris I like to use the truss command.  It is like the strace command under linux.

e.g.

truss date

Regards, Nisus
http://www.omnimodo.com
 truss -p ...
is the solution then, but keep in mind that it only traces system calls
ASKER CERTIFIED SOLUTION
Avatar of Nisus091197
Nisus091197

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, as nisus says, truss -f

But ...

A daemon process should not itself be a session or process group leader.  It is usual for the things to be competely disassociated from the any controlling tty (hence the session leader step), however the first tty device opened by a session or process group leader which hasn't already got a ctty, will become the controlling tty for that session or pgrp.  This is resolved by using ONOCTTY flag, or traditionally, before ONOCTTY,

  parent process forks
   first child setpgrp/setsid
   first child closes all open fd's
   first child forks
     second childs runs as daemon
   first child exits
  parent exits

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for experts of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.11473240.html
https://www.experts-exchange.com/questions/Q.11667758.html
https://www.experts-exchange.com/questions/Q.20076226.html
https://www.experts-exchange.com/questions/Q.20107139.html
https://www.experts-exchange.com/questions/Q.20109162.html
https://www.experts-exchange.com/questions/Q.20109691.html
https://www.experts-exchange.com/questions/Q.20118121.html
https://www.experts-exchange.com/questions/Q.20118127.html
https://www.experts-exchange.com/questions/Q.20173258.html
https://www.experts-exchange.com/questions/Q.20240985.html
https://www.experts-exchange.com/questions/Q.20258498.html
https://www.experts-exchange.com/questions/Q.20260199.html
https://www.experts-exchange.com/questions/Q.11263994.html
https://www.experts-exchange.com/questions/Q.20270368.html
https://www.experts-exchange.com/questions/Q.20271874.html
https://www.experts-exchange.com/questions/Q.20274619.html
https://www.experts-exchange.com/questions/Q.20276268.html
https://www.experts-exchange.com/questions/Q.20276453.html
https://www.experts-exchange.com/questions/Q.20276277.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20271075.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Hi kgreddy,

Has my suggestion been helpful?  Anything more we can help with?
Sorry Asker chose not to respond.  I finalized this today.
EE Moderator
very festive.

Have EE stopped giving away Tshirts?  I asked for my guru one ages ago?
T-shirt program went away when the site was sold.  The new owners are looking at new incentive programs.  Check the "What's New" for an update now and again.  The current focus is a site-wide rewrite to fix many of the inherited roadblocks/problems.  When new incentive programs are added, they will be announced in the What's New category.
Moondancer - EE Moderator