Re: Weird shell script behavior in a cron job
Hi.
On Wed, Aug 30, 2017 at 11:07:36AM -0700, James H. H. Lampert wrote:
> 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:
> > #!
A curious shebang.
> > 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
What about this approach?
date >> ~/test.txt
pwd >> ~/test.txt
/bin/mountpoint -q /media/ExternalHD/Backups || \
mount /media/ExternalHD && \
cd /media/ExternalHD/Backups
pwd >> ~/test.txt
> 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?
Presumably your script runs via /bin/bash in interactive mode, and via
/bin/sh (should be /bin/dash) if run by cron.
Reco
Reply to: