Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created June 10, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbietunnie/6ce31a665a81ea57ea64687a446ba85e to your computer and use it in GitHub Desktop.
Save barbietunnie/6ce31a665a81ea57ea64687a446ba85e to your computer and use it in GitHub Desktop.
Crontab Notes

Crontab Notes

  • Environments like Ubuntu use /bin/sh for executing crontab scripts rather than /bin/bash used by CentOS. To avoid failure with your crontab scripts, you can add

    SHELL=/bin/bash
    

    to the beginning of your crontab file to change the shell accordingly. Commands like source fail when /bin/sh shell is used.

    Many distributions use /bin/bash as the shell and it supports commands like source. On Ubuntu, however, /bin/dash is used which does not support source. Most shells use . instead of source. If you cannot edit your script, try to change the shell that runs it.

    Source

  • The working directory varies in different environments. To ensure your cron task is executed in the correct working directory, you should include the command to change to the desired directory before executing your script or dependent scripts. ALternatively, you can include it in your crontab command, e.g.

    58 23 * * * cd /home/xxxx/desired-dir/scripts && ./my_script.sh
    
  • How to fix No MTA installed errors in /var.log/syslog: By default, the output of each cron job is collected by cron and sent via email when the job is complete. If there is no output, no mail is sent.

    If you see (CRON) info (No MTA installed, discarding output) errors in your syslog it means that there is output from your cron job but your server does not have a Message Transfer Agent installed to process the output into an email.

    This is not a problem with your cron job itself but with the system.

    To fix this error,

    • Install an MTA like the popular postfix. This can be installed in most cases from a package manager. For example, on Ubuntu you may run:

      sudo apt-get install postfix
      

      If you select local installation, the output from your cron jobs will be relayed to a local "mailbox" that you can easily tail:

      sudo tail -f /var/mail/<cron user>
      

      Another option, if you don't care about cron emails, is to silence the error by disabling emails from your crontab. The easiest way to do this for all cron jobs is to add this to the top of the crontab file:

      MAILTO=""
      

      Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment