Search code examples
linuxdatebusybox

Embedded Linux: No output from date when stdout directed to file or FIFO


I am trying to pass the date into a file e.g.:

/pwd # date > file.txt

but every time I check the file, nothing is printed in there. I have tried other variants as well e.g.:

/pwd # echo "$(date)" > file.txt

/pwd # echo "$(/bin/date)" > /full/path/file.txt

/pwd # echo "$(/bin/date)" >> /full/path/file.txt (I wouldn't expect appending to make a difference but tried anyway)

I thought well maybe it was some sort of permissions issue with date being able to access the file so, for kicks, I tried:

/pwd # sudo date > /full/path/file.txt

Attempting to store the date to bash variable is futile as well. But, sure enough, the date command by itself continues to work:

Mon Apr 5 14:16:26 UTC 2021

Anyone have any ideas what could be happening? According to every other post I've read, the things I am trying should be working.

Note this is a special (proprietary) kernel (uname -a):

enter image description here

However it's built on Debian so I would expect it to work.

EDIT: type date:

date is a tracked alias for /bin/date

Nothing interesting happens when prefixing command with set -x other than the tracking I see when running the command:

enter image description here

strace looks like it has some good information but I won't pretend to know how to interpret it:

enter image description here

EDIT 2: enter image description here

enter image description here


Solution

  • This is an unusual situation -- from the strace results, it looks like either a libc bug or a busybox problem. Since this is a decade-old software stack, you don't have the recently-introduced date formatting printf builtin bash added in 4.3.

    The easiest workaround is to not use the busybox date command at all, and instead to switch to a scripting language -- Python, Perl, etc. @jhnc was kind enough to provide a perl command for the purpose:

    perl -e '$|=1; print scalar localtime, "\n"' > file.txt