LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-15-2017, 01:46 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
How do i mute STDOUT of the bash script executed by the crontab file?


Hello,

i have CentOS Linux and a file inside /etc/cron.d/crons

That fle contains this line:
Quote:
51 0 */7 * * root /bin/sh /root/scripts/script 1>/dev/null
The /root/scripts/script contains:
Quote:
echo " something $(hostname)" >> /tmp/output
When the cron runs the script, i receive an e-mail with that echoed content ("something my.hostname") even in crontab i attempt to redirect script STDOUT to /dev/null

I want to mute all STDOUT of that script but not mute STDERR

Currently i am using 1>/dev/null
I think i can use 2>&1 > /dev/null
but currently used redirect (1>/dev/null) is more simple, not sure why not work, what is the most simple command to redirect it the way i need?

Last edited by postcd; 06-15-2017 at 01:51 AM.
 
Old 06-15-2017, 04:14 AM   #2
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by postcd View Post
Hello,
I think i can use 2>&1 > /dev/null
Do it. You will get an email whenever there is some not redirected output either from stdout or stderr. So you have to redirect both in order not to receive mails regardless of the channel the output comes through.

But you need to reorder redirection parts in the following way.
Code:
... > /dev/null 2>&1
Otherwise stderr is to the direction of stdout when the stdout is not yet redirected to /dev/null.

Last edited by sweepnine; 06-15-2017 at 12:20 PM.
 
Old 06-15-2017, 08:54 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by postcd View Post

Currently i am using 1>/dev/null
I think i can use 2>&1 > /dev/null
but currently used redirect (1>/dev/null) is more simple, not sure why not work, what is the most simple command to redirect it the way i need?
File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).
/dev/null is an imaginary place that everything not wanted is sent to. You already know how to redirect.
 
1 members found this post helpful.
Old 06-18-2017, 11:38 PM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by postcd View Post
I want to mute all STDOUT of that script but not mute STDERR
You can redirect stdout and stderr separately as I demonstrate below in an interactive bash shell. You can do this in a script too.
Code:
$ touch temp.txt
$ ls temp.txt tmep.txt 1>> stdout.log 2>> stderr.log
$ cat stdout.log
temp.txt
$ cat stderr.log
ls: cannot access tmep.txt: No such file or directory
$
The 1 in 1>> is optional. You could use >> just as well.

You can "mute" either stdout or stderr by redirecting it to /dev/null.

You can "mute" both with the method which sweepnine showed.
 
1 members found this post helpful.
Old 06-19-2017, 12:35 AM   #5
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
I still don't understand why cron is emailing the output to you. Anything I redirect in the script never appears in the crontab's output. Anything I redirect in the crontab never appears in my email. If I redirect in both the script and crontab, the script alone takes it.

Does the output of your script also appear in /tmp/output, as the script is intended to redirect it?

Last edited by Beryllos; 06-19-2017 at 01:01 AM. Reason: I added more detail plus a question.
 
Old 06-19-2017, 03:06 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,630

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
probably that is another crontab
 
2 members found this post helpful.
Old 07-04-2017, 11:13 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Quote:
Originally Posted by Beryllos View Post
I still don't understand why cron is emailing the output to you. Anything I redirect in the script never appears in the crontab's output. Anything I redirect in the crontab never appears in my email. If I redirect in both the script and crontab, the script alone takes it.

Does the output of your script also appear in /tmp/output, as the script is intended to redirect it?
...and...
Quote:
Originally Posted by pan64
probably that is another crontab
There is a part of cron that you can define. By putting
Code:
MAILTO=user@server.com
..the output of any cron job will be sent along to the user specified. This could be set in /etc/crontab, but the OP doesn't tell us what version of CentOS they're using. First three hits in Google for "how to redirect stderr stdout in linux" are:
https://www.cyberciti.biz/faq/redire...err-to-stdout/
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
http://www.tldp.org/LDP/abs/html/io-redirection.html
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script add new crontab when no user cron file is created aristosv Linux - Software 1 12-31-2015 08:12 AM
[SOLVED] Problem with crontab:: command not executed properly via crontab Ankush Seth Linux - Newbie 11 11-11-2013 06:25 AM
echo commands in BASH script to STDOUT tospo Programming 9 11-03-2010 10:12 AM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 11:53 AM
redirecting BASH script stdout/stderr from the script itself Hewson Linux - General 4 04-18-2008 03:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:47 AM.

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