[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Weird shell script behavior in a cron job



Can somebody explain this:

My backup script WILL detect that ExternalHD is not mounted, and attempt to mount it, if I run it manually.

But it WON'T do that if it runs in a cron job.

I've isolated the relevant code into its own script, added debugging output, and set it up to run every minute. Here's the test script:
#!
date >> ~/test.txt
pwd >> ~/test.txt
cd /media/ExternalHD/Backups
if [ "$?" = "1" ]; then
  echo "mounting" >> ~/test.txt
  mount /media/ExternalHD >> ~/test.txt
  cd /media/ExternalHD/Backups
fi
pwd >> ~/test.txt

Here is what I get when the cron job trips, and ExternalHD is not mounted:
Wed Aug 30 10:49:01 PDT 2017
/root
/root
Wed Aug 30 10:50:01 PDT 2017
/root
/root
. . .
Wed Aug 30 10:55:01 PDT 2017
/root
/root

and here is what I get when I run the script from a command line:
Wed Aug 30 10:55:07 PDT 2017
/root
mounting
/media/ExternalHD/Backups

Why would the behavior be any different? Could it be that cron is running it an entirely different shell, that doesn't understand the "if" statement? Here's the crontab line:
* * * * *   ~/test.sh

--
JHHL


Reply to: