solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

cron job error?

Tue Nov 22, 2016 4:06 pm

I have a shell script that uses a mapfile command.

Code: Select all

#!/bin/sh
cd /home/pi/WeatherCam/

cp /home/pi/MOTION/lastsnap.jpg  /home/pi/WeatherCam/testpic.jpg

mapfile all_lines < "/var/www/weewx/temp.txt"

a="${all_lines[1]}"
b="${all_lines[3]}"
c="${all_lines[4]}"
d="${all_lines[6]}"  
It works great when run from the command line as in:

Code: Select all

 ./imageoverlay.sh
However when I run it through a crontab I get this:

Code: Select all

/home/pi/WeatherCam/imageoverlay.sh: 10: /home/pi/WeatherCam/imageoverlay.sh: mapfile: not found
/home/pi/WeatherCam/imageoverlay.sh: 15: /home/pi/WeatherCam/imageoverlay.sh: Bad substitution 
Why the mapfile not found?
Thanks for any and all insight!
I'm a total novice, non-programer (...basically a hack.)

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: cron job error?

Tue Nov 22, 2016 4:17 pm

full paths to everything

the environment is not the same under cron
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: cron job error?

Tue Nov 22, 2016 4:25 pm

RaTTuS wrote:full paths to everything

the environment is not the same under cron
Humm... isn't

Code: Select all

/home/pi/WeatherCam/imageoverlay.sh 
or

Code: Select all

/var/www/weewx/temp.txt  
the full path?
I'm a total novice, non-programer (...basically a hack.)

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: cron job error?

Tue Nov 22, 2016 4:28 pm

mapfile all_lines < "/var/www/weewx/temp.txt"
is not
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: cron job error?

Tue Nov 22, 2016 4:31 pm

RaTTuS wrote:
mapfile all_lines < "/var/www/weewx/temp.txt"
is not
Ok... How do I identify the full path?
I'm a total novice, non-programer (...basically a hack.)

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: cron job error?

Tue Nov 22, 2016 5:54 pm

mapfile is a built-in bash command, it's not available in sh which is the shell you have your script running in unless you have a mapfile program somewhere.
To find the full path to a command you can use which

Code: Select all

$ which mapfile
She who travels light — forgot something.

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: cron job error?

Tue Nov 22, 2016 6:09 pm

Paeryn wrote:mapfile is a built-in bash command, it's not available in sh which is the shell you have your script running in unless you have a mapfile program somewhere.
To find the full path to a command you can use which

Code: Select all

$ which mapfile
Thanks for taking the time to respond!
I'm even more confused now!
The mapfile command seems to run properly when I execute the shell script from the command line (so I must have mapfile program somewhere!?)... no errors. If /var/www/weewx/temp.txt is not the full path... what is?
I'm a total novice, non-programer (...basically a hack.)

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: cron job error?

Tue Nov 22, 2016 8:37 pm

When you run from the command line, the /bin/sh defaults to bash.

When run from cron, it uses dash instead (which is smaller and can be slightly faster).

You can force your script to use bash by changing the first line to

Code: Select all

#!/bin/bash

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: cron job error?

Tue Nov 22, 2016 8:59 pm

There's something wrong with the analysis here. But first note that, yes, it is certainly correct that the fix is to put /bin/bash in the "shebang" line. That should fix it.

But here are two things that have been stated in this thread that I do not believe:

1) That /bin/sh is (or can be) different things depending on whether or not you are running from cron. Unless you are using chroot or some other fancy thing, /bin/sh is what it is. And, yes, /bin/sh is a link to "dash".

2) That the OP is able to run this script correctly from the command line. Observe:

~ $ cat foo
#!/bin/sh
mapfile
~ $ chmod +x foo
~ $ ./foo
./foo: 2: ./foo: mapfile: not found
~ $

This shows clearly that if you run a script (from a bash prompt) whose shebang line is /bin/sh, you can't use "mapfile" in the script.

My guess is that OP was "dotting" the script rather than executing it...
If this post appears in the wrong forums category, my apologies.

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: cron job error?

Tue Nov 22, 2016 9:25 pm

ok... I changed it to a bash script

Code: Select all

#!/bin/bash 
and it now runs from the crontab and executes successfully.
THANK YOU... THANK YOU... THANK YOU!

...however, for what ever reason... when I ran it from the command line before I changed it from #!/bin/sh... I ran without error!

Again THANKS for all the suggestions!
I'm a total novice, non-programer (...basically a hack.)

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: cron job error?

Tue Nov 22, 2016 9:51 pm

solo2500 wrote:ok... I changed it to a bash script

Code: Select all

#!/bin/bash 
and it now runs from the crontab and executes successfully.
THANK YOU... THANK YOU... THANK YOU!

...however, for what ever reason... when I ran it from the command line before I changed it from #!/bin/sh... I ran without error!

Again THANKS for all the suggestions!
As Martin Frezman suggested earlier, were you actually sourcing the script originally rather than executing it, e.g.

Code: Select all

. ./imageoverlay.sh
Note the period on it's own at the start. That tells the shell to read the file and execute the commands in the current shell, in which case the shebang line at the top of the script would be ignored as it's technically a comment (it only means something to the system when trying to execute a file).
She who travels light — forgot something.

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: cron job error?

Tue Nov 22, 2016 10:03 pm

Paeryn wrote:
solo2500 wrote:ok... I changed it to a bash script

Code: Select all

#!/bin/bash 
and it now runs from the crontab and executes successfully.
THANK YOU... THANK YOU... THANK YOU!

...however, for what ever reason... when I ran it from the command line before I changed it from #!/bin/sh... I ran without error!

Again THANKS for all the suggestions!
As Martin Frezman suggested earlier, were you actually sourcing the script originally rather than executing it, e.g.

Code: Select all

. ./imageoverlay.sh
Note the period on it's own at the start. That tells the shell to read the file and execute the commands in the current shell, in which case the shebang line at the top of the script would be ignored as it's technically a comment (it only means something to the system when trying to execute a file).
Interesting! I didn't know that... however I was just using

Code: Select all

  ./imageoverlay.sh
to run it!?
I'm a total novice, non-programer (...basically a hack.)

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: cron job error?

Wed Nov 23, 2016 8:19 am

you learn something new each day ... I've never used mapfile before {AFAIK]
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Beginners”