My bash script generates a log file but not when it's executed via crontab.

Why is that?

Crontab entry.

55 0 * * * ~/scripts/BackupPC.sh >/dev/null 2>&1

I know that crontab is working because I checked /var/log/syslog

$ cat syslog | grep BackupRpi.sh
Jul  7 00:55:01 rpi CRON[13455]: (pi) CMD (sh ~/scripts/BackupRpi.sh >/dev/null 2>&1)

My script. The log file is created when I execute it manually via BackupRpi.sh

fn=${logsdir}/RPI-`date +%I-%M-%S-%p--%d-%b-%Y`.txt

echo "[START]  $(date)" >> $fn
rsync -avXP --delete-excluded --info=progress2 --no-i-r -R --log-file=${fn} ${HOME}  /media/hitachi/backup/rpi
echo "[END]  $(date)" >> $fn

  • 1
    When troubleshooting, redirecting output (especially error output) to /dev/null just hides what's going wrong. Try redirecting it to a log file instead, something like >/tmp/BackupRpi.log 2>&1 – Gordon Davisson Jul 7 at 6:55
  • 1
    The variable fn is defined using the variable logsdir: This variable is unknow to the system and hence it will attempt to write the output to /RPI-xxxx.txt – kvantour Jul 7 at 7:14
  • @kvantour You're right. The variable $logsdir is set in my .bashrc file via export logsdir=~/logs – Bassam Jul 7 at 18:01
  • @kvantour Any idea how I would make that variable available to the crontab environment? I'd rather not set it in the script itself (the logging does work when I declare $logsdir in the script itself). – Bassam Jul 7 at 19:18
  • So I'm sourcing my bashrc which sets $logsdir but the log is still not being generated.1 0 * * * source /home/pi/.bashrc; ~/scripts/BackupRpi.sh >/dev/null 2>&1 – Bassam Jul 13 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.