0

I am using nano 2.7.4 and have created a file to receive sensor data to table in SQL. I'm able to run the task form the command line and receive the correct data back in the database. However, the program will not run on its own.

  • 1
    Hello and welcome. I think something got lost during posting the question, i.e. that script. Does the crontab contain any uncommented lines? – Ghanima Aug 17 '18 at 18:51
  • ... also the last line in the crontab is duplicated – Greenonline Aug 17 '18 at 19:12
  • I checked that and it's not in the file. Must have been when I copied it. Sorry. – 00BEAR Aug 17 '18 at 19:13
  • No, it didn't copy right. All lines include #. – 00BEAR Aug 17 '18 at 19:16
  • what you can do is check the cron log files for any errors – Jaromanda X Aug 18 '18 at 1:18
1

Your crontab contains only commented lines... include at least one line without the #, likely that last line, which should read:

/10 * * * * /home/pi/tempLog/readTempSQL.py

instead of

#/10 * * * * /home/pi/tempLog/readTempSQL.py

Sidenote:

Per this page mind that:

Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

Sidenote 2:

The shebang of your script should have another slash

#!/usr/bin/env

instead of

#! usr/bin/env
  • Thanks for your help. I will remove the the "/"? Would "* * * * *" mean every minute? – 00BEAR Aug 17 '18 at 20:12
  • @00BEAR I would give that repeat pattern a try, the site does not claim that it will not work, only that it might not work on certain systems. Crontab will complain if it does not. – Ghanima Aug 17 '18 at 20:17
  • Ok, no problem with "/", I still receive back the data, but only if I run it. I'm using ~$ sudo chmod +x readTempSQL.py to execute? I this ok? – 00BEAR Aug 17 '18 at 20:21
  • Have you tried Seamus' advice? Redirecting error output (which otherwise would be discarded by cron) could give useful hints, provided the script generates helpful error messages. chmod does not execute a file, it changes the permission so that it becomes executable. Either way (+x or not) you can always run it the way that Seamus' answer suggests by calling the Python interpreter and handing over the scripts name. – Ghanima Aug 17 '18 at 20:29
1

As @Ghanima has said, you must first remove the comment character (#) in the line that you wish to execute (and probably the leading /). But you may also need to make another change before that will execute - you will need to indicate how you want your Python script executed. So, your revised line will look like this:

10 * * * * /usr/bin/python3 /home/pi/tempLog/readTempSQL.py 

I have assumed that your script runs under Python 3. If instead you wish to use Python 2, change the python path accordingly.

cron does not have the same $PATH as you do (as user pi), and so you must provide that information. One other thing that might help is to redirect any stderr output (errors) to a file as this may be useful in debugging any issues that occur. If you wish to do this:

10 * * * * /usr/bin/python3 /home/pi/tempLog/readTempSQL.py  > /home/pi/cronjoblog 2>&1
  • Comments are not for extended discussion; this conversation has been moved to chat. I understand that you and the OP are using the comments to solve the problem but I hope that a chatroom will work too. Feel free to update your answer - and the OP the question, respectively - with any new input nonetheless. Thanks. – Ghanima Aug 18 '18 at 19:14
0

Remove the hash at the start of the line. Hash is used to comment out a line.

Add python after the * and before the address path.

Add & after the address path to allow background tasks.

  • Such as this? 10 * * * * python /usr/bin/python3 & /home/pi/tempLog/readTempSQL.py > /home/pi/cronjob$ – 00BEAR Aug 17 '18 at 21:29
  • no, the & would go at the end of a command line, not half way in the middle – Jaromanda X Aug 18 '18 at 1:17
  • 10 * * * * python /home/pi/tempLog/readTempSQL.py > /home/pi/cronjob$ & – Andy Anderson Aug 18 '18 at 13:38
  • There is no need for the ampersand with cron, despite it being a myth repeatedly told on Stackexchange and all over the internet. – Ghanima Aug 18 '18 at 17:08

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.