0

I just created a tiny shell script to be run every minute.

What happened was that it started flooding my mail.

$ crontab -l
* * * * * /home/user/bin/test 2>&1 >/dev/null

I changed the redirection to >/dev/null 2>&1, and it stopped.

I thought 2>&1 >/dev/null and >/dev/null 2>&1 are equal in shell at least... Now I hesitate, are they (equal) and it's just the CRON programmed this way?

Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
  • 1
    See [Order of redirections](https://unix.stackexchange.com/questions/37660/order-of-redirections) (although be aware that cronjobs are more likely to be run in /bin/sh than bash). – steeldriver Feb 23 '22 at 00:33

1 Answers1

1

Those two commands are not the same.

1>stdout filename must be located in between the command and 2>&1


$ command  1>  stdoutput_filename  2>&1 

2> => redirect stderr, &1 => means stdoutput_filename.

$ command 2>&1 > /dev/null => &1 does not mean /dev/null

Example

$ ls issue issue.net nofile 2>&1 > /tmp/testfile
ls: cannot access nofile: No such file or directory
$ cat /tmp/testfile
issue
issue.net
$ ls issue issue.net nofile 1> /tmp/testfile 2>&1
$ cat /tmp/testfile
ls: cannot access nofile: No such file or directory
issue
issue.net
Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
firstcolor
  • 11
  • 1
  • 1
    It doesn't have to between the two, it just has to before `2>&1`. It can be before the command. – muru Feb 23 '22 at 11:51