Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #186200

Weird shell script behavior in a cron job

From "James H. H. Lampert" <jamesl@touchtonecorp.com>
Newsgroups linux.debian.user
Subject Weird shell script behavior in a cron job
Date 2017-08-30 20:10 +0200
Message-ID <ukfB9-3fk-39@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


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

Back to linux.debian.user | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Weird shell script behavior in a cron job "James H. H. Lampert" <jamesl@touchtonecorp.com> - 2017-08-30 20:10 +0200
  Re: Weird shell script behavior in a cron job "James H. H. Lampert" <jamesl@touchtonecorp.com> - 2017-08-30 20:40 +0200
    Re: Weird shell script behavior in a cron job David Margerison <deltam@electrosonics.com.au> - 2017-09-01 16:50 +0200
  Re: Weird shell script behavior in a cron job Reco <recoverym4n@gmail.com> - 2017-08-30 20:40 +0200
    Re: Weird shell script behavior in a cron job Greg Wooledge <wooledg@eeg.ccf.org> - 2017-08-30 20:50 +0200
  Re: Weird shell script behavior in a cron job David Wright <deblis@lionunicorn.co.uk> - 2017-08-30 20:50 +0200
  Re: Weird shell script behavior in a cron job Jonathan de Boyne Pollard <J.deBoynePollard-newsgroups@NTLWorld.COM> - 2017-08-31 08:10 +0200
    Re: Weird shell script behavior in a cron job Greg Wooledge <wooledg@eeg.ccf.org> - 2017-08-31 14:10 +0200
      Re: Weird shell script behavior in a cron job Reco <recoverym4n@gmail.com> - 2017-08-31 14:20 +0200
        Re: Weird shell script behavior in a cron job "James H. H. Lampert" <jamesl@touchtonecorp.com> - 2017-08-31 17:40 +0200

csiph-web