Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Script doesn't run via crontab -e but works fine standalone

Thu Mar 17, 2016 11:38 am

Hi Raspberry friends,

My script (for temperature reporting to my Domoticz server) doesn't run via crontab -e but works fine standalone. All files used for this script (temper.txt, AdafruitDHT.py and dht22.sh) are chmodded to 777. Could you guys maybe help me?

I already tried adding "sudo" before the file.

Added line in crontab -e:

Code: Select all

*/5 * * * * /home/pi/domoticz/scripts/dht22.sh
dht22.sh script:

Code: Select all

#!/bin/bash

# Domoticz server
SERVER="192.168.0.105:8080"

# DHT IDX
# le numéro de l IDX dans la liste des peripheriques
DHTIDX="32"
#DHTPIN
# LE GPIO ou est connecte le DHT11
DHTPIN="27"

# si vous avez un DHT22 modifiez plus bas sur la ligne Adafruit_DHT 11 par Adafruit_DHT 22
# TMPFILE : chemin pour fichier temporaire a placer dans le RAMDRIVE pour eviter les
# ecritures sur la SD card
# sinon chemin ou sera ecrit le fichier contenant les temperature
# /tmp/temper.txt est un bon choix si pas de RAMDRIVE installe
# consultez www.easydomoticz.com pour tout savoir

TMPFILE="/home/pi/var/tmp/temper.txt"
# modif de patrick du 08/03/15 pour interroger que 5 fois maxi
cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""
sleep 5
sudo nice -20 home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
if [ $TEMP ]
then
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
HUM=$(cat $TMPFILE |grep Temp |awk '{print $7}')
#echo $TEMP
#echo $HUM
# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htm?type=command&param=udevice&idx$
TEMP=""
HUM=""
rm $TMPFILE
exit 0
fi
#echo $cpt
cpt=$(($cpt+1))
done
exit 1
Thank you very much in advance for any comments / thoughts.

Regards,

Cognac

DirkS
Posts: 10398
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Script doesn't run via crontab -e but works fine standal

Thu Mar 17, 2016 11:53 am

How do you know the script is not started by cron?

I can see at least one problem in your script:

Code: Select all

sudo nice -20 home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
You're missing the / at the start of the path to the python script.

You also cannot assume that the python interpreter can be found if you run the script from cron (unless the shebang contains the full path to python).

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

Re: Script doesn't run via crontab -e but works fine standal

Thu Mar 17, 2016 12:03 pm

DirkS wrote:How do you know the script is not started by cron?

I can see at least one problem in your script:

Code: Select all

sudo nice -20 home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
You're missing the / at the start of the path to the python script.

You also cannot assume that the python interpreter can be found if you run the script from cron (unless the shebang contains the full path to python).
also if you need to run it as sudo then put it in the root crontab
sudo crontab -e
don't have the sudo in the script
or more properly, remove the need to run it as sudo
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

Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Re: Script doesn't run via crontab -e but works fine standal

Thu Mar 17, 2016 12:05 pm

Hi DirkS,

Thank you for your quick reply.

You are right, I do not know if the script is started by cron. Fact is that I receive output when executing the script standalone. Using the Cron no output (every 5 minutes) is received.

I also tested:

Code: Select all

sudo nice -20 ./Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
as path to the python script in my dht22.py script without succes.

Will test your suggestion this evening. Hopefully it works.

Regards,

Cognac.

Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Re: Script doesn't run via crontab -e but works fine standal

Thu Mar 17, 2016 12:09 pm

RaTTuS wrote: also if you need to run it as sudo then put it in the root crontab
sudo crontab -e
don't have the sudo in the script
or more properly, remove the need to run it as sudo
Thank you! Will also try this! I tested the added line in the root crontab but also under the /etc/crontab -e without succes.

Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 5:33 am

I have tried all options mentioned above. Thank you.

Unfortunately the scipt is still not running. :cry: I really don't get it.

The strange thing is that I have this script and the script it uses installed on another Raspberry Pi. On the other Rpi it works like a charm. Same scripts, same file locations, same permissions of the files.....

Also standlone the dht22.sh script works fine. Who can help me solving this crontab issue? :)

My current settings are:

Text in crontab (file obtained in /etc/crontab -e):

Code: Select all

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
*/5 * * * * /home/pi/domoticz/scripts/dht22.sh
dht22.sh script:

Code: Select all

#!/bin/bash

# Domoticz server
SERVER="192.168.0.105:8080"

# DHT IDX
# le numéro de l IDX dans la liste des peripheriques
DHTIDX="32"
#DHTPIN
# LE GPIO ou est connecte le DHT11
DHTPIN="27"

# si vous avez un DHT22 modifiez plus bas sur la ligne Adafruit_DHT 11 par Adafruit_DHT 22
# TMPFILE : chemin pour fichier temporaire a placer dans le RAMDRIVE pour eviter les
# ecritures sur la SD card
# sinon chemin ou sera ecrit le fichier contenant les temperature
# /tmp/temper.txt est un bon choix si pas de RAMDRIVE installe
# consultez www.easydomoticz.com pour tout savoir

TMPFILE="/tmp/temper.txt"
# modif de patrick du 08/03/15 pour interroger que 5 fois maxi
cpt=0
while [ $cpt -lt 6 ]
do
TEMP=""
sleep 5
sudo nice -20 ./Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
if [ $TEMP ]
then
TEMP=$(cat $TMPFILE|grep Temp |awk '{print $3}')
HUM=$(cat $TMPFILE |grep Temp |awk '{print $7}')
#echo $TEMP
#echo $HUM
# Send data
curl -s -i -H "Accept: application/json" "http://$SERVER/json.htmtype=command&param=udevice&idx=$DHTIDX&nvalue=0&svalue=$TEMP;$HUM;2"
TEMP=""
HUM=""
rm $TMPFILE
exit 0
fi
#echo $cpt
cpt=$(($cpt+1))
done
exit 1

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 5:57 am

See this note in the cron...

..... These files also have username fields, that none of the other crontabs do.....

I think it wants you to add a username to the 6th field

Also, as someone else already mentioned.. this line should be the full pathname...

sudo nice -20 ./Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE

Regards,
-Moses
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 6:24 am

Thank you Moses!

I just added "pi" to the crontab:

Code: Select all

*/5 * * * *     pi      /home/pi/domoticz/scripts/dht22.sh
And changed the line:

Code: Select all

sudo nice -20 /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
Standalone the script still works fine. Via the crontab the script do not seem to work.

Hopefully you know some other options :D

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 6:34 am

Redirect the output of your script to a file.. that way you can see what is going on. This should work;

Change the cron to..

*/5 * * * * pi /home/pi/domoticz/scripts/dht22.sh >> /home/pi/domoticz/scripts/CRON.OUTPUT 2>&1

This redirected both the std error and std out to the CRON.OUTPUT file
Let it run and check the file /home/pi/domoticz/scripts/CRON.OUTPUT

Regards,
-Moses
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

Cognac
Posts: 8
Joined: Tue Mar 15, 2016 11:16 am

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 7:11 am

Cognac wrote:Thank you Moses!

I just added "pi" to the crontab:

Code: Select all

*/5 * * * *     pi      /home/pi/domoticz/scripts/dht22.sh
And changed the line:

Code: Select all

sudo nice -20 /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 22 $DHTPIN > $TMPFILE
Standalone the script still works fine. Via the crontab the script do not seem to work.
Hopefully you know some other options :D
The crontab finally works. The above changes and REBOOTING the pi worked for me.

@ Moses, RaTTus & DirkS Thank you!

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

Re: Script doesn't run via crontab -e but works fine standal

Fri Mar 18, 2016 8:53 am

you should really have in in the user crontab
i.e.
crontab -e
not the system one .. but as it now works...
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 “Troubleshooting”