3

My problem is that crontab is not executing my php script /home/pi/CronTabFiles/saveThermometer.php
I tried a lot of things, I don't remember everyone of them but I'll list what i consider mains one.

-This is how my basic crontab line looks like:
mm hh * * * /usr/bin/php /home/pi/CronTabFiles/saveThermometer.php
home, pi and CronTabFiles folders have set permissions to 777.
This result in an email with this text: Could not open input file: /home/pi/CronTabFiles/saveThermometer.php
- I tried to make a simple .sh file with this code

#! /bin/sh
php /home/pi/CronTabFiles/saveThermometer.php

and this results in /bin/sh: 1: /home/test.sh
: not found

- I tried also wget, curl and lynx but they didn't work as well

I will take any help that will make crontab executing my php or shell scripts.

Edit 1: Permisions to save thermometer.php are also set to 777.
Edit 2: I tried www-data and root crontab, both didn't work. My crontab is only executing simple commands such as gpio write 0 1.
When I manually do this command:

php /home/pi/CronTabFiles/saveThermometer.php

Everything works well.

1

I added

 > /tmp/result 

at the end of my crontab line and everything works fine.

  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review – Dougie Jan 21 at 19:20
  • I'm the author, I don't know why this works but it worked for me. – ksiadz510 Jan 22 at 20:04
  • Are you sure you added "> /tmp result" ?? It would redirect output to the /tmp folder, It should result in an error "/tmp: cannot create [is a map]". – Gerard H. Pille Jan 22 at 20:18
  • I'm really sorry, I edited my answer, I forget slash. – ksiadz510 Jan 24 at 17:18
0

Unfortunately the suggested 'fix' does not really explain why the script is failing.

My suggestion would be to launch your script in a screen session so that you can:

  1. Log the console output to a file for later review
  2. Attach to the screen session after booting up

GNU screen may already be installed on your system but run sudo apt-get install screen to be sure.

So your crontab entry would look like this:

mm hh * * * /usr/bin/screen -L -Logfile /home/pi/cron.log -dmS autostart bash -c '/usr/bin/php /home/pi/CronTabFiles/saveThermometer.php; exec bash '

You may wonder what the exec bash at the end does, it is to keep the console active after your script has finished running.

NB: I think your script should be run as root to access the GPIO, so I would add it to the root crontab.

Then restart your PI, log in to SSH as root (remember the script will be run as root), then type screen -ls to verify that you have one screen session active (you should, provided the command is correct). Then you can just type screen -r to join the session.

You should also have a log file in /home/pi/cron.log. Thus you should be able to see the output of your script as if it were run from the command line.

It is also possible that for some reason your PHP script is crashing in a non-interactive context, for example relying on some environment variable that is not there etc. It goes without saying that a good script should handle exceptions and log them to a file. Even a minimalist try/catch (10 lines of code) will save you a lot of headache and alert you to errors that may otherwise go unnoticed. If don't have proper exception handling built-in - now is a good time to add it to your script to make it more robust and reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.