M BUZZ CRAZE NEWS
// general

Bash script output on the screen also to the log file

By Daniel Rodriguez

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>&1

so 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

2

1 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 $LOGFILE

This 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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy