Bash script output on the screen also to the log file
I created a simple script that does a curl command ( I run it every month via crontab ) and I made it echo to a logfile with the date of run. The problem is that I want to receive the shell script output as well to the log file and I'm sure quite sure how to achieve that. This is what I have:
#!/bin/bash
#
LOGFILE=/root/Delete-Old-Indices.log
#Nginx Logs - Delete older than 90 Days
curl -XDELETE XXXXXXXXXXXXXXXXXX
echo "$(date "+%d%m%Y %T") : Starting work" >> $LOGFILE 2>&1so now I only receive the following line of log in my log file: "Date + Time : Starting work" and I don't get the shell script output in my log file, I only see it in my shell window.
Thank you
21 Answer
You can redirect any output to the log file the way you did for the echo command, i.e., using the >> redirection symbol.
That would cause no output to be displayed anymore on the screen. If you wish to also see the output, you can use tee with the -a or --append option, as
curl -XDELETE XXXXXXXXXXXXXXXXXX | tee -a $LOGFILEThis redirects both to the file (appending to what is already there), and the screen.
If what you want is to collect all output from the script into a log file, then simply redirect the output while invoking the shell script. Within the script itself, you would then remove all redirection.
2More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"