-1

I am having a problem in daemonize program. The problem is after closing all the opened descriptors, i need to reopen the stdout file to print the message.

I am having one way. But that is not working.

The way is duplicate the stdout descriptor using dup and reopen that. But the deamonize function is called it closes all the file descriptors. So, that duplicate file descriptor is also closed.

Can anyone please help me to do that.

  • 1
    Why do you want to print to stdout in a daemon? – P.P Jan 18 '16 at 11:18
  • I want to check the program is running only once(single instance daemon). So, if it is already executed i need to print the error message in terminal. –  Jan 18 '16 at 11:19
  • This question may be related to [this other question](http://stackoverflow.com/questions/9084099/re-opening-stdout-and-stdin-file-descriptors-after-closing-them) – Magix Jan 18 '16 at 11:25
  • stack overflow link:http://stackoverflow.com/questions/9084099/re-opening-stdout-and-stdin-file-descriptors-after-closing-them – msc Jan 18 '16 at 11:26
  • 1
    @msc, It is not that question. My requirement is after closing all the file descriptors, i need to open the stdout file. –  Jan 18 '16 at 11:27
  • You can try to open `/dev/tty`. – jofel Jan 18 '16 at 11:28
  • 1
    It is not working. It gives the error "no such devices or address". –  Jan 18 '16 at 11:29
  • @suresh - see related link:http://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdo – msc Jan 18 '16 at 11:35
  • @suresh Do that before calling `daemon()`. – fuz Jan 18 '16 at 11:39
  • @msc, That is ok. But before doing that i Want to close all the opened file descriptor. So, that duplicate file descriptors also closed. –  Jan 18 '16 at 11:40

1 Answers1

1

If you use daemon() to daemonize, you can specify noclose to prevent these filedescriptors from being closed:

daemon(0, 1);

But you should close these after your check by hand, otherwise your terminals might get messed up.

Ctx
  • 18,090
  • 24
  • 36
  • 51