Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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
share|improve this answer
    
Its true that >&1 2>&1 is the analog to &>, but i think that in the portable cat case it is useless - if cat errors it should quit and write nothing anyway so >file is enough. –  mikeserv Mar 23 at 20:44
    
@mikeserv: Yes, you can see man bash, REDIRECTION section for more details. No, if cat produce any errors, these errors will be saved to file. –  Gnouc Mar 24 at 1:27
    
What kind of errors will be saved to the file? –  mikeserv Mar 24 at 1:28
    
@mikeserv: any errors produce by cat, i.e you cat a file which doesn't exist. –  Gnouc Mar 24 at 1:31
    
Much better than that test -e $file && cat $_ >$tgt –  mikeserv Mar 24 at 1:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.