Final logging considerations

You might consider having your script take care of its own logging. While this is certainly possible, (although a little complex and not very readable), we feel strongly that it is the responsibility of the caller to take care of logging. If you find a script that takes care of its own logging, you might encounter some of the following issues:

  • Multiple users run the same script at different intervals, to a single log file
  • The log file needs to have robust user permissions to ensure correct exposure
  • Both ad hoc and scheduled runs will appear in the log file

Simply put, delegating the responsibility of logging to the script itself is asking for trouble. For an ad hoc command, you get the output right in your Terminal. If you need it for any other purpose, you can always copy and paste it somewhere, or redirect it. Even more probable is running the script with a pipe to tee, so output is shown to your Terminal and saved to a file at the same time. For scheduled runs from cron, you need to think once about redirection: when you create the schedule. In this case, especially if you use the Bash 4.x construct of &>>, you will always see all output (stdout and stderr) appended to the file you specify. In this, there is almost no risk of missing any output. Remember: tee and redirections are your friends, and when used properly they make a great addition to any script scheduling!

If you want your cron logging mechanism to be really fancy, you can set up sendmail (or other software such as postfix) as an actual Mail Transfer Agent (very out of the scope of this book, but check the Further reading section!). If that is correctly configured, you can set the MAILTO variable in the crontab to an actual email address (perhaps [email protected]), and receive the reports from scheduled jobs in your normal email box. This is best used with important scripts that do not run too often; otherwise, you will just end up with an annoying amount of email.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.189.67