16

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?

Michael Frederick
  • 16,664
  • 3
  • 43
  • 58

3 Answers3

27

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'
smac89
  • 39,374
  • 15
  • 132
  • 179
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 9
    N.B. you can also use `&>` to redirect both at once. It works in zsh and bash. – Kevin Feb 20 '13 at 15:15
  • 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. – Michael Frederick Feb 20 '13 at 15:33
  • 2
    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. – dogbane Feb 20 '13 at 15:51
  • How to insert `YYYY-MM-DD_hh-mm-sec` into output file name, so that every file name is different and kept without rewriting? – Danijel Jan 11 '16 at 13:18
  • @Kevin, ``&>`` seem like not worked in ``Ubuntu 16.04.1 LTS `` */1 * * * * /mnt/data_disk_1/www/ershou_web/ershou_web_production/current/bin/pg_backup_rotated.sh &> /tmp/pg_backup_debug6.log, the output is nothing, this script is exist a shebang like this: ``#!/bin/bash`` – zw963 Apr 13 '17 at 12:55
  • 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! – Itamar Katz Jun 11 '17 at 07:49
2

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

Ilya I
  • 1,282
  • 1
  • 12
  • 19
  • 3
    N.B. you can also use `&>` to redirect both at once. It works in zsh and bash. – Kevin Feb 20 '13 at 15:13
  • 3
    @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 – Squidly Nov 26 '14 at 13:47
1

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'
Community
  • 1
  • 1
Johannes
  • 154
  • 4