I'm having trouble redirecting C printf statements to a file with a program running as root.
I have a program with several informational printf statements and other libraries which print errors via stderr. I'd like to log these to a file. If I start the program from the command line, I can see stderr and stdout messages.
sudo ./myprogram
If I try to redirect like so,
sudo ./myprogram >> log_file 2>&1
The log_file only contains stderr, and stdout disappears.
I suspect the issue is related to the root user not being connected to stdout, but I haven't found a solution.
My ultimate goal is to run this program at system start as root, while logging stderr and stdout to a log_file.
I've been trying this using crontab,
sudo crontab -e
and adding the line,
@reboot bin/myprogram >> log_file 2>&1
This generates stderr messages but stdout disappears, same as command line.
Any help is appreciated, thanks!
Edits & updates: Testing this morning, the redirection behaves the same without sudo. A typical printf statement looks like:
printf("info: passed safety check\n");
I'm using a default set up for ubuntu 14.04 on an ARM processor, so I believe I'm using bash. I confirmed with,
echo $SHELL
produces
/bin/bash
sudo
? Could you share a line of code that contains aprintf
statement? – kraxor Jul 29 '14 at 22:12bash
withsudo
andcron
? There are shells which do not understand this redirection syntax. And you could have different shells set up in these different contexts. – Run CMD Jul 30 '14 at 6:58stderr
generate nothing to output bystdout
!? – Pandya Jul 30 '14 at 12:30