Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically you need to handle STDERR in the same way as STDOUT.</p> <h2>Pipe it in cron</h2> <pre><code>0 0 * * * (/usr/bin/time /path/to/mysqlbackup.sh 2&amp;&gt;1) | /bin/mail -s "MySQL Backup" "admin@example.com" </code></pre> <p>This works on Ubuntu, you reported it doesn't work for you (empty mail body). I'd be interested to dig into that so more, but in the meantime...</p> <h2>Adjust your script</h2> <p>You can make your script only print to STDOUT by redirecting STDERR (at least for that one command to STDOUT)</p> <pre><code>echo "Starting backup at $(date)" mysql -u potato -e "show status" 2&gt;&amp;1 # produces error we want, so redirect it FILE="/path/to/file.bak" FILESIZE=`du -h $FILE | cut -f1` echo $FILESIZE </code></pre> <p>If you want any error in your script to be sent to STDOUT, you can globally redirect STDERR to STDOUT by adding this at the top of your script:</p> <pre><code>exec 2&gt;&amp;1 </code></pre> <h3>Toy example</h3> <p><code>job.sh</code> </p> <pre><code>#prints out to STDOUT and STDERR echo good echo error &gt;&amp;2 echo good </code></pre> <p><code>script.sh</code></p> <pre><code>#this runs job.sh redirecting STDERR to STDOUT sh job.sh 2&gt;&amp;1 </code></pre> <p>Now running job.sh, with various redirections shows its printing to STDOUT and STDERR</p> <pre><code>$sh job.sh &gt; /dev/null error $sh job.sh 2&gt; /dev/null good good </code></pre> <p>running script.sh shows its only printing to STDOUT even though job.sh is printing to STDERR</p> <pre><code>$sh script.sh &gt; /dev/null $sh script.sh 2&gt; /dev/null good error good </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload