Indranil Ganguly Indranil Ganguly - 4 months ago 17
Bash Question

How to see shell script execution using Cronjob

I am currently running a cron job to schedule my shell script after a particular interval but i am not able to see my shell script execution and the log how can i see it.
Thanks

Answer

You can use the redirection sign (>) in order to redirect the stdout and/or stderr to a log file.

For example:

0 0 * * * /path/to/shell-script.sh > /tmp/shell-script.log 2>&1

Where:

  • 0 0 * * * means the script will run every day at midnight
  • /path/to/shell-script.sh - the path of the script
  • > means redirect the stream to a file followed by the redirection sign
  • 2>&1 means

    • File descriptor 1 is the standard output (stdout).

    • File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1 may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1". & indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1.

Comments