Your cron
user has a different environment than your interactive shell (e.g. bash
) user.
Solutions:
- use absolute path statements:
/home/user/scriptname.sh
instead ofscriptname.sh
or./scriptname.sh
- be really clever, and set the
cron
user's environment: set env for cron; run cron with existing env ; more info in this answer on SE ; still more on SE - know the differences! Ask
cron
to tell you whatenvironment
it's using - here's how to ask.
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 undersystemd
!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 yourcron
jobs require an active, operational network service be available, you may wish to add the following to the[Unit]
section yourcron.service
file:After=network-online.target
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 capturestderr
&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 yourcrontab
Assuming you have an MTA installed on your system, you may insert the
MAILTO
variable in yourcrontab
to send email notifications to all yourcron
jobs - or to each one individually. Seeman 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:
- The crontab guru will help you with your schedule specification
cron
,crontab
... What are you talking about?- Q&A: How can I run a cron command with existing environmental variables?
- Crontab – Quick Reference
- Execution environment of a cron job - incl. the
MAILTO
variable - Q&A: How can I run a cron command with existing environmental variables? U&L SE
- Q&A: Where can I set environment variables that crontab will use? SO SE
- Linux Privilege Escalation by Exploiting Cronjobs
- Q&A: Is CRON secure? SF SE
- Q&A: How to set environment variable for everyone under my linux system? SO SE
- How to enable logging for cron on Linux
- Q&A: Where is the default terminal $PATH located on Mac? SO SE
- How To Read and Set Environmental and Shell Variables on a Linux VPS fm DigitalOcean.
- Set and List Environment Variables in Linux fm LinOxide
- How to Add to the Shell Path in macOS Catalina 10.5 using Terminal
- Crontab Email Settings ( MAILTO )
- Q&A: How to temporarily disable a user's cronjobs? - Yes, even
cron
is managed bysystemd
! - Why is my crontab not working, and how can I troubleshoot it? - a canonical question at serverfaultSE
- Why crontab scripts are not working? - a canonical question at askubuntuSE