Redirecting stderr to pipe from cron - linux

I'm trying to pipe stderr to logger with this code:
/usr/local/bin/Script.py >/dev/null 2>(/usr/bin/logger -t MyScript -p syslog.err)
This runs fine when run from a bash commandline but has no output in syslog when run from cron. This is my (root) crontab:
0-59/5 * * * * /usr/local/bin/Script.py >/dev/null 2>(/usr/bin/logger -t MyScript -p syslog.err)
Can anybody help and tell me What is going wrong here?
Thanks!

>/dev/null is redirecting both stdout/stderr to /dev/null before the 2> redirection can pick it up.
Instead, redirect stdout to /dev/null explicitly:
/usr/local/bin/Script.py 1>/dev/null 2>(/usr/bin/logger -t MyScript -p syslog.err)

Related

what does this error >/dev/null 2>/dev/null) mean

I have this error in logs file
CRON[3304]: (root) CMD ( /usr/bin/php /home/app/foxorders/scripts/restart_apache.php >/dev/null 2>/dev/null)
Whats mean please ?
This is not a error. It's just information about cron job which execute command:
/usr/bin/php /home/app/foxorders/scripts/restart_apache.php
and redirect STDOUT and STDERR to NULL device

command with exec and tee to redirect output to a file and console at the same time

Linux script has the command exec > &LOGFILE 2>&1 to redirect output to a file, I am trying to amend the command to redirect output to a file and also to the console at the same time.
None of the below command works. It either redirects output to file or writes to the console but not both at the same time. Can somebody help?
exec > >(tee -a $LOGFILE) 2>&1
exec &> >(tee -a $LOGFILE) 2>&1
exec >> $LOGFILE 2>&1 && tail -f $LOGFILE

How to prevent Cron Task from writing file on server?

I'm facing this problem :
When I execute a CRON task from my Web Server with this command :
wget https://mon-url.com/file/cron.php >/dev/null 2>&1
It creates new files cron.php1, cron.php2, ... in my /root directory and It takes a lot of space. How can I prevent this ?
Thanks
You can use -O /dev/null what will write the output file to /dev/null
wget -O /dev/null https://mon-url.com/file/cron.php >/dev/null 2>&1
Or -O- which will output it to stdout.
wget -O- https://mon-url.com/file/cron.php >/dev/null 2>&1
This is the answer:
wget --delete-after

Redirect stdout and stderr to file and stderr to stdout

The following writes stdout to a logfile and prints stderr:
bash script.sh >> out.log
This again writes both stdout and stderr to a logfile:
bash script.sh >> out.log 2>&1
How to combine both features, so that stdout and stderr are logged to a file and stderr is emailed to my inbox?
bash script.sh 2>&1 >> out.log | tee -a out.log
First, I'm redirecting stdout to file and stderr to stdout (stdout line gets to the out.log file and stderr to pipe).
The tee command prints stdin to both stdout and file (resemblance with the letter T). Thus second, I'm printing the original stderr to both stdout and the out.log file (the -a argument means append).
You can keep stdout in a separate file and stderr in separate file:
0 * * * * bash script.sh > out.log 2> err.log
and then email yourself err.log file.
Here is a working solution:
./myscript.sh &> all.txt 2> stderr.txt
&> all.txt to have both stderr and stdout
2> stderr.txt to have only stderr
And then just do whatever you want with those files, such as email logging for instance!
Using process substitution you can try:
0 * * * * bash script.sh >> out.log 2> >(exec tee >(exec cat >> mail))
Or
0 * * * * bash -c 'exec bash script.sh >> out.log 2> >(exec tee >(exec cat >> mail))'
exec cat >> mail imitates mailing. Replace it with a command that actually does the mailing.

How to redirect the output of an application in background to /dev/null

I would like to redirect the output generated from a background application in Linux to /dev/null.
I am using kate text editor and it prints all the debug messages on the terminal which I would like to redirect to /dev/null.
Any idea how to do it ?
Thanks
You use:
yourcommand > /dev/null 2>&1
If it should run in the Background add an &
yourcommand > /dev/null 2>&1 &
>/dev/null 2>&1 means redirect stdout to /dev/null AND stderr to the place where stdout points at that time
If you want stderr to occur on console and only stdout going to /dev/null you can use:
yourcommand 2>&1 > /dev/null
In this case stderr is redirected to stdout (e.g. your console) and afterwards the original stdout is redirected to /dev/null
If the program should not terminate you can use:
nohup yourcommand &
Without any parameter all output lands in nohup.out
These will also redirect both:
yourcommand &> /dev/null
yourcommand >& /dev/null
though the bash manual says the first is preferred.

Resources