LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   *BSD (http://www.linuxquestions.org/questions/%2Absd-17/)
-   -   Problems with logging the output of a custom daemon with a custom init script (http://www.linuxquestions.org/questions/%2Absd-17/problems-with-logging-the-output-of-a-custom-daemon-with-a-custom-init-script-804913/)

d1s4st3r 04-29-2010 04:17 AM

Problems with logging the output of a custom daemon with a custom init script
 
Hi guys,

I'm in the following situation: I'm working on a FreeBSD 8.0 machine and I have a custom init script for a service (let's call it "myservice").

Its path is
Code:

/usr/local/etc/rc.d/myservice
and it contains the following text
Code:

#!/bin/sh

. /etc/rc.subr

name="myservice"
rcvar=${name}_enable

load_rc_config ${name}

: ${myservice_enable="NO"}
: ${myservice_conf="/usr/local/etc/myservice.conf"}

required_files="${myservice_conf}"
command=/usr/local/sbin/myservice
command_args="-f ${myservice_conf}"

run_rc_command "$1" 2> /var/log/myservice.log

As you can see, in the last line I added "2> /var/log/myservice.log" so I can have a (kind of) log file (this because the service is a stupid one and doesn't implement a logging mechanism but simply writes everything on the console).

This solution works, and I have the "/var/log/myservice.log" log file with all the stderr messages I need.

Now, my problem is:

1) how can I log stdout too? If I use "&>" instead of "2>", the shell parses it as "& >" (note the space)

2) even if the text printed on the console by the service is *plain text*, my log file *always* begins with a lot of 0x00 characters (the number of those chars, always on the stop of file, seems to be increasing as the log file gets bigger). Infact, if I use the "file" command on the log file, I get "data" instead of "ASCII text". Very odd!

My question is: how can I log both stdout and stderr of that service to a file in the most simple way?

kbp 04-29-2010 09:29 PM

Usually './command > /logfile 2>&1' should do the trick...

d1s4st3r 04-30-2010 03:21 AM

Thanks, kbp, this seems to work.
Any advice about the other problem?
The log file is ok, but still contains a lot of NULL characters.
This is a little part of the output of "hexdump -v /var/log/myservice.log":
Code:

...
00000b0 3537 000a 0000 0000 0000 0000 0000 0000
00000c0 0000 0000 0000 0000 0000 0000 0000 0000
00000d0 0000 0000 0000 0000 0000 0000 0000 0000
00000e0 0000 0000 0000 0000 0000 0000 0000 0000
00000f0 0000 0000 0000 0000 0000 0000 0000 0000
0000100 0000 0000 0000 0000 0000 0000 0000 0000
0000110 0000 0000 0000 0000 0000 0000 0000 0000
0000120 0000 0000 0000 0000 0000 0000 0000 0000
0000130 0000 0000 0000 0000 0000 0000 0000 0000
0000140 0000 0000 0000 0000 0000 0000 0000 0000
0000150 0000 0000 0000 0000 0000 0000 0000 0000
0000160 0000 0000 0000 0000 0000 0000 0000 0000
0000170 0000 0000 0000 0000 0000 0000 0000 0000
0000180 0000 0000 0000 0000 0000 0000 0000 0000
0000190 0000 0000 0000 0000 0000 0000 0000 0000
00001a0 0000 0000 0000 0000 0000 0000 0000 0000
00001b0 0000 0000 0000 0000 0000 0000 0000 0000
...

That's very odd...
Thanks in advance! :-)

broken 04-30-2010 04:46 AM

The console doesn't/mustn't/shouldn't display null characters. According to http://en.wikipedia.org/wiki/Null_character :

Quote:

The original meaning of this character was like NOP—when sent to a printer or a terminal, it does nothing
This explains the discrepancy you have been experiencing. Your "custom daemon" is sending null characters to the standard output/error device(s).


All times are GMT -5. The time now is 05:29 AM.