Redirecting the output of a cron job

39,045

Solution 1

Your redirection order is incorrect. Stderr is not being redirected to the file, but is being sent to stdout. That's what you must be receiving in your mail.

Fix the redirection by changing your cron job to:

0 5 * * * /bin/bash -l -c
'export RAILS_ENV=my_env;
cd /my_folder;
./script/my_script.rb > ./log/my_log.log 2>&1'

Solution 2

Try swapping 2>&1 with > ./log/my_log.log.

Solution 3

Judging by this answer you just need to switch the order of the redirects:

0 5 * * * /bin/bash -l -c 'export RAILS_ENV=my_env; cd /my_folder; ./script/my_script.rb > ./log/my_log.log 2>&1'
Share:
39,045
Michael Frederick
Author by

Michael Frederick

i eat computers for breakfast

Updated on October 15, 2020

Comments

  • Michael Frederick
    Michael Frederick over 3 years

    I have the following entry in crontab:

    0 5 * * * /bin/bash -l -c 'export RAILS_ENV=my_env; cd /my_folder; ./script/my_script.rb 2>&1 > ./log/my_log.log'
    

    The result of this is that I am receiving the output of ./script/my_script.rb in ./log/my_log.log. This behavior is desired. What is curious is that I am also receiving the output in my local mail. I am wondering how the output of my script is being captured by mail. Since I am redirecting the output to a log file, I would expect that my cron job would have no output, and thus I would receive no mail when the cron job runs. Can anyone shed some light as to how mail is able to get the output of ./script/my_script.rb?

  • Kevin
    Kevin about 11 years
    N.B. you can also use &> to redirect both at once. It works in zsh and bash.
  • Kevin
    Kevin about 11 years
    N.B. you can also use &> to redirect both at once. It works in zsh and bash.
  • Michael Frederick
    Michael Frederick about 11 years
    I was under the impression that my syntax redirected stderr to stdout, and stdout to my log file, no? Can you explain a bit further? Thanks for the help.
  • dogbane
    dogbane about 11 years
    Yours redirected stderr to wherever stdout was going (i.e. /dev/stdout), and then stdout to your log file. Stderr doesn't change after the second redirection. It is still going to /dev/stdout.
  • Squidly
    Squidly over 9 years
    @Kevin if you do this, then you must set the SHELL in your crontab to be bash, otherwise it will (probably) use sh, and fail to work
  • Danijel
    Danijel over 8 years
    How to insert YYYY-MM-DD_hh-mm-sec into output file name, so that every file name is different and kept without rewriting?
  • zw963
    zw963 about 7 years
    @Kevin, &> seem like not worked in Ubuntu 16.04.1 LTS */1 * * * * /mnt/data_disk_1/www/ershou_web/ershou_web_production/curren‌​t/bin/pg_backup_rota‌​ted.sh &> /tmp/pg_backup_debug6.log, the output is nothing, this script is exist a shebang like this: #!/bin/bash
  • Itamar Katz
    Itamar Katz almost 7 years
    Hi, am trying to redirect the output of a command to a file on remote server, but no matter what I try, the file is created empty. any idea? I put /bin/bash before the command, I tried to put the command in a script and call it from the crontab, nothing helps. the command is `echo 'test' | ssh usr@server "cat >! test.txt" (the command works ok if I just execute it in the terminal. Thanks!