Reputation: 877
I am running the command "sudo dhcpcd eth0.2" and getting output in the shell However I cannot redirect this output anywhere.
I have tried both redirecting standard output and error output using:
>
>>
|
2>
> file 2>&1
| tee file
I have also tried going to root instead of using sudo.
This is the output I want to redirect
pi@raspberrypi:~ $ sudo dhcpcd eth0.2
DUID 00:01:00:01:23:4f:5f:0a:b8:27:eb:64:90:ac
eth0.2: IAID eb:31:c5:f9
eth0.2: soliciting a DHCP lease
eth0.2: soliciting an IPv6 router
eth0.2: using IPv4LL address 169.254.149.253
eth0.2: adding route to 169.254.0.0/16
forked to background, child pid 1883
pi@raspberrypi:~ $
Trying to send standard output to file
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 > tmp
pi@raspberrypi:~ $ cat tmp
pi@raspberrypi:~ $
Trying to send error output to file
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 2> tmp
DUID 00:01:00:01:23:4f:5f:0a:b8:27:eb:64:90:ac
eth0.2: IAID eb:31:c5:f9
eth0.2: soliciting a DHCP lease
eth0.2: soliciting an IPv6 router
eth0.2: using IPv4LL address 169.254.149.253
eth0.2: adding route to 169.254.0.0/16
forked to background, child pid 1948
pi@raspberrypi:~ $ cat tmp
pi@raspberrypi:~ $
Trying to pipe to tee
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 |tee tmp
pi@raspberrypi:~ $ cat tmp
pi@raspberrypi:~ $
Trying >>
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 >> tmp
pi@raspberrypi:~ $ cat tmp
pi@raspberrypi:~ $
Trying grep
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 |egrep .
pi@raspberrypi:~ $
pi@raspberrypi:~ $ stat tmp
File: tmp
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: b307h/45831d Inode: 1184 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ pi) Gid: ( 1000/ pi)
Access: 2019-04-17 10:55:44.351058825 +0200
Modify: 2019-04-17 11:13:29.936779857 +0200
Change: 2019-04-17 11:13:29.936779857 +0200
Birth: -
pi@raspberrypi:~ $ sudo dhcpcd eth0.2 > tmp 2>&1
pi@raspberrypi:~ $ stat tmp
File: tmp
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: b307h/45831d Inode: 1184 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ pi) Gid: ( 1000/ pi)
Access: 2019-04-17 10:55:44.351058825 +0200
Modify: 2019-04-17 11:15:26.295156264 +0200
Change: 2019-04-17 11:15:26.295156264 +0200
Birth: -
pi@raspberrypi:~ $
Same when running as root without sudo
root@raspberrypi:~# dhcpcd eth0.2 > tmp
root@raspberrypi:~# cat tmp
root@raspberrypi:~#
What we can see is that the output is no longer shown on console when I redirect standard output, while it does show when I redirect error output. That should mean it is standard output. But why is not caught by any redirects?
The expected result is that I should be able to send the text shown on console to a file or other process.
Upvotes: 0
Views: 235
Reputation: 158270
dhcpd is running(!) in background and must not necessarily have flushed the output when you check the file.
Try:
sudo stdbuf -oL -eL dhcpcd eth0.2 >> tmp
When the process is performing output operations (e.g. printf()
) the underlying libc will buffer the output. Depending on whether the output goes to a terminal or not, the buffering algorithm will be line-buffered (tty) or block buffered (non-tty).
The buffer size for block buffering depends on your system. In your example, the output was smaller than the buffer size and therefore the file was still empty when you looked at it.
Upvotes: 1