Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-11-2009, 10:08 AM   #1
Senior Member
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
I got a question about redirecting to /dev/null

I was looking at my crontab and it has this

40 4 * * * /usr/bin/run-parts /etc/cron.daily 1> /dev/null
This line of code redirects STDOUT to /dev/null, correct? Why would cron have that statement instead of this one.

40 4 * * * /usr/bin/run-parts /etc/cron.daily > /dev/null
Isn't that the same thing?

Also, what's the difference between

0 * * * * /usr/bin/run-parts /etc/cron.hourly > /dev/null 2>&1

0 * * * * /usr/bin/run-parts /etc/cron.hourly >> /dev/null 2>&1
Does it really matter if u use '>' vs '>>' to /dev/null. I know that '>' means to overwrite while '>>' means to append but why would you want to append instead of overwrite if you're throwing the output away?

Last edited by trist007; 12-11-2009 at 10:17 AM.
Old 12-11-2009, 10:20 AM   #2
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
1> means to redirect standard output from the program. 2> would redirect error output.

Cron uses 1> so that errors are not thrown away. The 2>&1 then redirects any errors to the standard output (i.e. the screen).

Using >> wouldn't make a difference over > when redirecting to /dev/null, but cron isn't using > it's using 1> which is different for the reason explained above.

Last edited by Komakino; 12-11-2009 at 10:22 AM.
Old 12-11-2009, 10:32 AM   #3
Senior Member
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,765
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
extra details often helpful months (minutes) later

When your redirect output using '>>' you append to an existing file. In contrast when you use '>' you create a file.

Why would someone write "1> someFile.log" instead of "> someFile.log"?
They do the same thing. I prefer to write more details, including explicitly stating default behavior, when I write configuration files or scripts that I won't visit for months [er, minutes (grin)]. That way I don't need to remember what the defaults are or even chase down default behavior when I need to maintain things. In your case, I suspect that the cron config may have had "1> someFile.log 2>&1" or similar and this got edited along the road. This is a trick that captures both stdout and stderr into the same file. It says, "send file #1 (stdout) to the file and send file #2 (stderr) to file #1.

Hope this helps,
~~~ 0;-Dan
Old 12-13-2009, 07:34 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,340

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
prog >file

prog 1>file

do the same thing ie stdout (aka channel 1) is implied if not specified.

In a normal unix process creation, 3 I/O chans are attached:
0 = stdin
1 = stdout
2 = stderr

2>&1 means send stderr to the same place as stdout.

prog >p.log 2>&1

means send stdout to p.log and send stderr to the same place (p.log).

In the case of /dev/null, there is no difference in practical terms between output to (>) and append (>>). This is obviously not true for real files.

Last edited by chrism01; 12-13-2009 at 07:35 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: /dev/null And /dev/zero On Linux And Unix: What's The Difference? LXer Syndicated Linux News 0 04-29-2009 11:50 AM
Redirecting a desktop-oriented program's mandatory X output to /dev/null for a server srunni Linux - Server 3 07-16-2008 06:16 PM
Startx Permission problems on /dev/null and /dev/mem on freshly compiled Eric_Cartman Linux - Kernel 2 09-09-2007 02:42 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 11:51 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 03:36 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration