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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded‚Ķ
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

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