kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 10:32 am

What I am trying to accomplish

Good day all.

I am working on a project where when I my PI sends GPS coordinates received to a webservice. If the PI is rebooted, a cron runs and calls my python script to start the gpsd service and start sending my coordinates to my server again.

What I have done so far

So far, my Pi receives coords, and I have done the scriipt to return coordinates to my server. It's basically the code used in this link http://www.danmandle.com/blog/getting-g ... th-python/
but I have edited it to post a request instead of writing to the console and edited the sleep timer so it is called once every 60 seconds. So pretty much everything is "more less" the same".

Problem

My problem is, when the Pi reboots, this cron job isnt run or fails silently, I have changed the reboot job to another script which does a simple request to my web service and even that does not work. So my pi doesnt begin uploading GPS data until i explicitly open a terminal and run the script myself.

More Information
In the efforts of full disclosure,
I am using the Ultimate GPS Hat from Adafruit which needs this code

Code: Select all

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
to run to start the gpsd daemon.
so I have placed this in rc.local.

If this code is not run the gpsd doesn't receive coords and nothing is uploaded. I dont know if this is a reason for my cron job not being run or something. However, I doubt this before because I created this simple script

Command

Code: Select all

@reboot python /home/pi/uprequest.py 
Content

Code: Select all

#! /bin/bash
import requests

payload={'dogic':"8",'lat':'122','lon':'30'}
r= requests.post("http://my-link.com/trackingPI/uploadLoc.php",data=payload)
yet still it doesnt work on reboot (but works when i run manually or set to run every 5 minutes).


I have a plan B in mind, where I remove the 60 seconds timer from the script and I call it once every minute in a cron job (since im sure that works) instead of having it to call in reboot. However, when I remove the timer, instead of just returning me one result and end, it returns like 50 when i run it once and sometimes 5 in the very same second.
I feel this way is safer cause if something goes wrong and the script isnt called in one minute it is still called in the second instance, vs if it is only called once on reboot and something goes wrong. it is never called again.

I realize that this may be two questions in one however just trying to find a working method so, any help getting this done would be appreciated
Thank you.

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

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 10:38 am

kemnet wrote:...

Code: Select all

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
to run to start the gpsd daemon.
so I have placed this in rc.local.
[/code]
if it is rc.lcoal then you do not need sudo

Code: Select all

#! /bin/bash
import requests
if it is python then use the correct #!
i.e.
#!/bin/python
or whereever it is

as to why it is not working ....
all sorts of things
check dmseg to see that all is good on reboot
also check
/var/log/syslog
/var/log/messages
for anything that may be important
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

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 1:49 pm

RaTTuS wrote:
kemnet wrote:...

Code: Select all

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
to run to start the gpsd daemon.
so I have placed this in rc.local.
[/code]
if it is rc.lcoal then you do not need sudo

Code: Select all

#! /bin/bash
import requests
if it is python then use the correct #!
i.e.
#!/bin/python
or whereever it is

as to why it is not working ....
all sorts of things
check dmseg to see that all is good on reboot
also check
/var/log/syslog
/var/log/messages
for anything that may be important

I have removed sudo, that part works fine.

I have tried the shebang you suggest and also tried

Code: Select all

# /usr/bin/env python
but still nothing, my @reboot doesn't get called.

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

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 1:57 pm

no spaces in the #! line

in your script - output to a file /tmp/log or something to see if it works
it may be that it is running too soon and something else is not initiated [network ??]
put more debugging in your scripts
or
use systemd and restart it after the system is up
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

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 2:41 pm

RaTTuS wrote:no spaces in the #! line

in your script - output to a file /tmp/log or something to see if it works
it may be that it is running too soon and something else is not initiated [network ??]
put more debugging in your scripts
or
use systemd and restart it after the system is up
I think i debunked the theory that the network might not be initialized?
cause I tried writing to a file on boot and that doesn't work either.

but they work in timmed jobs and if I run it normally.
Code

Code: Select all

#!/usr/bin/ python
tfile = open("myfile.txt","wb")
tfile.write("Please work");
tfile.close()
Cron job

Code: Select all

@reboot sudo /usr/bin/python3 /home/pi/lock.py
@reboot python /home/pi/lock.py
#tried both of those

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 2:50 pm

Logs I write to are showing up blank, system logs have no errors, and the cron reboot doesnt run no matter how simple it is or what ever combination I try.
Have you gotten a reboot working on your pi? cause im seeing other people having these issues.

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

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 2:59 pm

cron @reboot works fine
^ put full path in that write
/tmp/log.log
expose errors to a known terminal /dev/tty9 ?
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

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 3:12 pm

RaTTuS wrote:cron @reboot works fine
^ put full path in that write
/tmp/log.log
expose errors to a known terminal /dev/tty9 ?
Thanks for your assistance however, I'm having a little trouble understanding what you are reffering to, my linux jagon isnt up to par.

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 5:54 pm

Cron Code

Code: Select all

@reboot sudo /usr/bin/python /home/pi/lock.py > /var/log/myError.log 2>&1
Lock.py code

Code: Select all

#!/usr/bin/env python
fo = open("open.txt","wb")
fo.write("please work")
fo.close()
Then I run

Code: Select all

cat /var/log/myError.log
At the end of the day, the file isnt created on reboot and no errors are written to the log.

User avatar
jojopi
Posts: 3270
Joined: Tue Oct 11, 2011 8:38 pm

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 6:07 pm

Normal users cannot write to /var/log. sudo runs after shell redirections are set up, so it is too late to help with >/var/log/myError.log.

If your program really needs root privilege then put it in root's crontab using "sudo crontab -e". Otherwise drop the sudo and put the log file somewhere else.

By the way, if you are still claiming that specific times work but @reboot does not, then it can only be that the job is running too early with respect to networking or something else.

kemnet
Posts: 26
Joined: Sat Aug 10, 2013 12:11 am

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 6:18 pm

jojopi wrote:Normal users cannot write to /var/log. sudo runs after shell redirections are set up, so it is too late to help with >/var/log/myError.log.

If your program really needs root privilege then put it in root's crontab using "sudo crontab -e". Otherwise drop the sudo and put the log file somewhere else.

By the way, if you are still claiming that specific times work but @reboot does not, then it can only be that the job is running too early with respect to networking or something else.
Hi, can you give me a snippet that you have somewhere where @reboot works? cause i've been struggling all morning to see a py get called on my reboot.

User avatar
jojopi
Posts: 3270
Joined: Tue Oct 11, 2011 8:38 pm

Re: Reboot cron job NOT running to start GPS

Mon Apr 11, 2016 6:54 pm

kemnet wrote:Hi, can you give me a snippet that you have somewhere where @reboot works? cause i've been struggling all morning to see a py get called on my reboot.
Yes, I have just retested this:

Code: Select all

pi@raspberrypi ~ $ crontab -l |tail -1
@reboot date >/home/pi/REBOOT
pi@raspberrypi ~ $ cat /home/pi/REBOOT
Mon 11 Apr 18:48:58 UTC 2016
pi@raspberrypi ~ $ uptime
 18:53:57 up 5 min,  1 user,  load average: 0.03, 0.26, 0.16

Return to “General programming discussion”