1

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 1

3

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
  • I was going to suggest using the redirection symbol >> in the crontab line with the bash script, but I like this solution better.
    – user68186
    Jun 9, 2021 at 16:37
  • 1
    @user68186 you are right, and that is probably more what the user wants in this specific case: running from a cron tab, one never sees the screen output. Added this to the answer
    – vanadium
    Jun 10, 2021 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .