Kit Fisto
Kit Fisto

Reputation: 4515

Ok to write to stdout on Unix process without terminal?

I want to be sure that the following will not compromise my process:

The Solaris program writes heavily to stdout (via C++ wcout stream). The output serves for tracing, so during testing and analyisis the programmer/tester can easily observe what happens. But the program is actually a server process, so in the production version it will run as a demon without attached console and write all the trace output to files.

I assume that stdout is redirected to nul for a program without console, in this case I guess all is fine. However I want to be sure that the stdout output is not buffered somewhere such that after sufficient run-time we could have memory or disk space problems.

Note: we cannot redirect the trace output to a file because this would grow too large. Instead our own file tracing mechanism makes sure that new files are created and old ones deleted to always keep a certain amount of tracing and not more.

Upvotes: 0

Views: 461

Answers (2)

James Kanze
James Kanze

Reputation: 153977

It depends on how the daemon is started. If it's started as a cron job, the output will be captured and mailed to whoever owns the crontab entry, unless you redirect the output in the command line. (But programs started as cron jobs aren't truly daemons.)

More generally, all processes are started from another program (except the init processes); most of the time, that program is a shell (even crontab invokes a shell to start its jobs), and the command is given as a command line. And you can redirect the output anywhere you please in a command line; /dev/null is a popular choice for cases like yours. Most daemons are started from an rc file; a shell script installed under /etc/rcn.d. Just redirect your output there.

Or better yet, rewrite your code to use some form of rotating logs, instead of standard out.

Upvotes: 0

January
January

Reputation: 17110

That depends how the daemon is started, I guess. When the daemon process is created, the streams have to be taken care of somehow (for example, they need to be detached from the current process, least the daemon would have to be terminated when the shell from which it was started manually exits).

Upvotes: 2

Related Questions