Skip to content

Latest commit

 

History

History
97 lines (61 loc) · 6.27 KB

MyCrontabDoesntWork.md

File metadata and controls

97 lines (61 loc) · 6.27 KB

Q: Why does my crontab not work?

A: cron is simple in some respects, but enigmatic in others:

Issue #1: the PATH environment variable is different.

Your cron user has a different environment than your interactive shell (e.g. bash) user.

Solutions:

Issue #2: cron has no awareness of the state of other system services.

This is typically only an issue when using the @reboot scheduling facility in cron - before all the system's other services are available. When services are managed under systemd, system service dependencies (e.g. network is available) may be declared in a "unit file", but cron is designed to support any program.

Solutions:

  • sleep before starting a script with service dependencies:

    @reboot ( /bin/sleep 30; /bin/bash /home/pi/startup.sh )
  • write your program/script to check availability of required resources

  • cron itself is a service, and yes - it may be managed under systemd!

    The service file for cron is found in /lib/systemd/system/cron.service. You may modify this file to add dependencies. For example: If many of your cron jobs require an active, operational network service be available, you may wish to add the following to the [Unit] section your cron.service file:

    After=network-online.target
    

Issue #3: cron output goes to /dev/null

New users sometimes wonder why they don't see any output at the terminal from their cron jobs - as they did when they ran the same program from their interactive shell (e.g. bash).

Solutions:

  • redirect the output of your cron job to capture stderr & stdout to a file:

    @reboot ( /bin/sleep 30; /bin/bash /home/pi/startup.sh > /home/pi/cronjoblog 2>&1)
  • enable logging for cron

    Edit the file /etc/rsyslog.conf to remove the comment (#) from #cron:

    FROM:  #cron.*                         /var/log/cron.log  
    
    TO:     cron.*                         /var/log/cron.log
  • use the MAILTO variable in your crontab

    Assuming you have an MTA installed on your system, you may insert the MAILTO variable in your crontab to send email notifications to all your cron jobs - or to each one individually. See man 5 crontab for details, or peruse the references listed below.

    If you want to install an MTA for local use only, the package exim4-daemon-light will serve that purpose :

    $ sudo apt-get install exim4-daemon-light

REFERENCES:

  1. The crontab guru will help you with your schedule specification
  2. cron, crontab... What are you talking about? 
  3. Q&A: How can I run a cron command with existing environmental variables?
  4. Crontab – Quick Reference
  5. Execution environment of a cron job - incl. the MAILTO variable
  6. Q&A: How can I run a cron command with existing environmental variables? U&L SE
  7. Q&A: Where can I set environment variables that crontab will use? SO SE
  8. Linux Privilege Escalation by Exploiting Cronjobs
  9. Q&A: Is CRON secure? SF SE
  10. Q&A: How to set environment variable for everyone under my linux system? SO SE
  11. How to enable logging for cron on Linux
  12. Q&A: Where is the default terminal $PATH located on Mac? SO SE
  13. How To Read and Set Environmental and Shell Variables on a Linux VPS fm DigitalOcean.
  14. Set and List Environment Variables in Linux fm LinOxide
  15. How to Add to the Shell Path in macOS Catalina 10.5 using Terminal
  16. Crontab Email Settings ( MAILTO )
  17. Q&A: How to temporarily disable a user's cronjobs? - Yes, even cron is managed by systemd!
  18. Why is my crontab not working, and how can I troubleshoot it? - a canonical question at serverfaultSE
  19. Why crontab scripts are not working? - a canonical question at askubuntuSE