How to run a program as daemon ?

Posted on 2002-03-07
Last Modified: 2013-12-27
I have an application which should run as daemon when user specifies -d option.
I am doing the following to make it daemon.

2)If successful, exit parent
3)make child as session leader
4)cd /

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
Question by:kgreddy
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
  • +1
LVL 51

Expert Comment

ID: 6848499
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.

Expert Comment

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


truss date

Regards, Nisus
LVL 51

Expert Comment

ID: 6851290
 truss -p ...
is the solution then, but keep in mind that it only traces system calls
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.


Accepted Solution

Nisus091197 earned 50 total points
ID: 6852036
truss -f -p pid-of-process

The -f will follow child processes.

That should do the trick.

Regards, Nisus

Expert Comment

ID: 6855068
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


Expert Comment

ID: 6892775
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.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.

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.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

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

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.
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.

Expert Comment

ID: 6915667
Hi kgreddy,

Has my suggestion been helpful?  Anything more we can help with?

Expert Comment

ID: 6940829
Sorry Asker chose not to respond.  I finalized this today.
EE Moderator

Expert Comment

ID: 6940946
very festive.

Have EE stopped giving away Tshirts?  I asked for my guru one ages ago?

Expert Comment

ID: 6958456
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

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

695 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