4

System: Xubuntu 13.10

When I have this crontab entry

*/5 * * * * cat /home/dbk/.bash_aliases &> /home/dbk/Desktop/junk

junk has a byte size of 0.

Running

$ cat /home/dbk/.bash_aliases &> /home/dbk/Desktop/junk

gives a file with a proper size and content.

DK Bose
  • 947

1 Answers1

5

The probem because cron run task with sh. &> is a shortcut to redirect both stderr and stdout to the same file in bash, not in sh.

In sh, your command:

cat /home/dbk/.bash_aliases &> /home/dbk/Desktop/junk

meaning run two commands separately:

  • Run cat /home/dbk/.bash_aliases in background

    cat /home/dbk/.bash_aliases &

  • Truncate the junk file.

    > /home/dbk/Desktop/junk

So you should use bash to run your command in crontab:

*/5 * * * * bash -c "cat /home/dbk/.bash_aliases &> /home/dbk/Desktop/junk"

or using more portable way:

*/5 * * * * cat /home/dbk/.bash_aliases > /home/dbk/Desktop/junk 2>&1
cuonglm
  • 153,898