How to redirect stderr to a file and some echos (not stdout) to the same file

cyberpirate92

I am learning bash scripting. I am trying to write a simple script that runs apt-get update in regular intervals using crontab for scheduling. I am trying to send the errors (with the time the error occurred )to a log file but I am only getting the error and not the time,

The script file ,

echo "----------   $(date +"%d %B %G") , $(date +%r) :";
apt-get update;

The crontab file (sudo crontab -e)

0 */5 * * * /root/scripts/update_sys.sh 2>/root/logs/updateLog 1>/dev/null

as far as I understood the echo statement in the script file dumps the text to the standard output, thus it ends up in /dev/null, but how could I include it in the log file without the entire info ( I mean info that apt-get dumps like Hit http://in.archive.ubuntu.com precise-backports/main Sources ) ?

alvits

If you can modify the script but not the crontab then the simplest is to redirect the echoes in the script to stderr.

echo "----------   $(date +"%d %B %G") , $(date +%r) :" >&2

All stdout will still go to /dev/null and all stderr will be logged along with the echoes to stderr.

If you can modify the crontab but not the script, then the simplest would be devnull's suggestion.

If you can modify either, then you have a choice.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to redirect and append both stdout and stderr to a file with Bash?

From Java

Redirect Windows cmd stdout and stderr to a single file

From Java

How to redirect output to a file and stdout

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Dev

Trouble using dup2 to redirect stdout/stderr into the same file

From Dev

How to prepend timestamp to the STDERR and redirect to a file

From Dev

How to append constantly changing stderr to a file or stdout

From Dev

How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

From Dev

redirect stdout and stderr to one file, copy of just stderr to another

From Dev

How to redirect stderr to a file in a cron job

From Dev

Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

From Dev

Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

From Dev

bash: redirect stderr to file and stdout + stderr to screen

From Dev

Redirect bash stdout+stderr to one file and stderr to another file

From Dev

Redirect stdout/stderr of a background job from console to a log file?

From Dev

How to redirect stderr to a file and some echos (not stdout) to the same file

From Dev

How to redirect STDERR to both file and console and STDOUT to file only

From Dev

How to redirect stdout to a file, and stdout+stderr to another one?

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Dev

How to redirect stdout and stderr to a file and display stderr to console?

From Dev

Redirect script stderr & stdout to a file, but keep stdout to tty as well?

From Dev

How to redirect jmeter stdout to a file

From Dev

Unable to redirect stdout/stderr to log file

From Dev

How to redirect stdin and stdout and stderr at the same time in bash?

From Dev

Redirect stdout / stderr output to log file with timestamp

From Dev

Append stderr and stdout to file

From Dev

How to save stdout to file, stderr to file, stdout+stderr to file and get stdout + stderr to terminal like normal for a shell script

From Dev

POSIX compliant way to redirect stdout and stderr to a file

From Dev

Laravel artisan queue:work redirect stderr/stdout to file

Related Related

  1. 1

    How to redirect and append both stdout and stderr to a file with Bash?

  2. 2

    Redirect Windows cmd stdout and stderr to a single file

  3. 3

    How to redirect output to a file and stdout

  4. 4

    Redirect stdout and stderr to file and stderr to stdout

  5. 5

    Trouble using dup2 to redirect stdout/stderr into the same file

  6. 6

    How to prepend timestamp to the STDERR and redirect to a file

  7. 7

    How to append constantly changing stderr to a file or stdout

  8. 8

    How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

  9. 9

    redirect stdout and stderr to one file, copy of just stderr to another

  10. 10

    How to redirect stderr to a file in a cron job

  11. 11

    Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

  12. 12

    Is it safe to redirect stdout and stderr to the same file without file descriptor copies?

  13. 13

    bash: redirect stderr to file and stdout + stderr to screen

  14. 14

    Redirect bash stdout+stderr to one file and stderr to another file

  15. 15

    Redirect stdout/stderr of a background job from console to a log file?

  16. 16

    How to redirect stderr to a file and some echos (not stdout) to the same file

  17. 17

    How to redirect STDERR to both file and console and STDOUT to file only

  18. 18

    How to redirect stdout to a file, and stdout+stderr to another one?

  19. 19

    Redirect stdout and stderr to file and stderr to stdout

  20. 20

    How to redirect stdout and stderr to a file and display stderr to console?

  21. 21

    Redirect script stderr & stdout to a file, but keep stdout to tty as well?

  22. 22

    How to redirect jmeter stdout to a file

  23. 23

    Unable to redirect stdout/stderr to log file

  24. 24

    How to redirect stdin and stdout and stderr at the same time in bash?

  25. 25

    Redirect stdout / stderr output to log file with timestamp

  26. 26

    Append stderr and stdout to file

  27. 27

    How to save stdout to file, stderr to file, stdout+stderr to file and get stdout + stderr to terminal like normal for a shell script

  28. 28

    POSIX compliant way to redirect stdout and stderr to a file

  29. 29

    Laravel artisan queue:work redirect stderr/stdout to file

HotTag

Archive