How to redirect complete output of a cron script

前端 未结 4 525
星月不相逢
星月不相逢 2021-01-03 07:13

I have a simple cronjob running every day at 18:35:

05 18 * * * ~/job.sh 2>&1 >> ~/job.log

So the output of ~/job.sh should be

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 07:47

    Arkaitz has the simplest solution. However, to see what's wrong with your snippet we need to go into the bash manual:

    Note that the order of redirections is significant. For example, the command

      ls > dirlist 2>&1
    

    directs both standard output and standard error to the file dirlist, while the command

     ls 2>&1 > dirlist
    

    directs only the standard output to file dirlist, because the standard error was duplicated from the standard output before the standard out‐ put was redirected to dirlist.

    So apparently the output redirection only redirects to the target of the other stream, not to the other stream itself. When bash parses your command line it will come upon the 2>&1 clause and redirect stderr to the target of stdout, which at this point is still the console (or whatever is attached to cron's stdout). Only after this will stdout be redirected.

    So what you really want is:

    05 18 * * * ~/job.sh >>~/job.log 2>&1
    

提交回复
热议问题