nunocv
Posts: 11
Joined: Thu Jun 08, 2017 11:35 pm

python and crontab

Sat Oct 21, 2017 1:29 am

Hello,
i'm trying to get the startup time of my raspi, the time that i turn it on.
i create this python script called 'boot_time.py'

Code: Select all

from time import strftime
with open("/home/pi/monitor/boot_time.txt", "w") as log:
  log.write(strftime("%d-%m-%Y,%H:%M:%S"))
the code works ok if execute the python file. it register the date and time that i executed the script

so i created a crontab entry so the script would run on boot
so i executed

Code: Select all

crontab -e
and added the following line

Code: Select all

@reboot python /home/pi/monitor/boot_time.py &
the thing is that on very first boot it doens't work
it only works when i reboot the system
hope that one of you guys can help me figure out what's wrong

pcmanbob
Posts: 2060
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: python and crontab

Sat Oct 21, 2017 1:08 pm

Hi.

So using a pi2b running Jessie lite with a wired connection and a fixed IP on the pi, ran your code using the cron line

Code: Select all

@reboot python /home/pi/boot_time.py
and it works perfectly on reboot and a cold start.

So then I tried it with no network connection and it still works but you just get the saved time set by the fake HW clock.

So I don't see why it wont work for you may be some more details which OS and what network connection would help with us trying to reproduce your problem.

Edit.... repeated using same pi but using stretch lite and a fixed IP on the pi same results still works ok.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

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

Re: python and crontab

Sat Oct 21, 2017 2:53 pm

nunocv wrote:
Sat Oct 21, 2017 1:29 am

Code: Select all

crontab -e
and added the following line

Code: Select all

@reboot python /home/pi/monitor/boot_time.py &
You don't need the "&" on a line in crontab. The code always runs in the background.

To diagnose errors when running you can direct the output to a file like this:

Code: Select all

@reboot python /home/pi/monitor/boot_time.py >/tmp/myprog.log 2>/tmp/myprog.err
That should create two files when the cron job triggers. /tmp/myprog.log, which has any standard output from the script and /tmp/myprog.err which will contain any error mssages.

nunocv
Posts: 11
Joined: Thu Jun 08, 2017 11:35 pm

Re: python and crontab

Sun Oct 22, 2017 12:39 am

thanks for your reply guys.
pcmanbob you said that:
So then I tried it with no network connection and it still works but you just get the saved time set by the fake HW clock
what does this mean?

rpdom i removed the '&' and added the 'log' and 'err', but they both came clean
i think the problem may be related to what pcmanbob said about 'fake HW clock', but i need the learn the real meaning of this

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

Re: python and crontab

Sun Oct 22, 2017 4:55 am

nunocv wrote:
Sun Oct 22, 2017 12:39 am
i think the problem may be related to what pcmanbob said about 'fake HW clock', but i need the learn the real meaning of this
Unlike most computers, the Pi doesn't have an onboard clock with battery power. This means that when it is switched off it forgets the time and date completely.

To get around this the Pi uses two methods.

One is to set the time from an internet time server when it starts up. That only works when it has an internet connection.

The other is to use a program called fake-hwclock which logs the current time in a file every hour (at 17 minutes past the hour, usually), and also during shutdown. When the Pi starts up it reads that file and sets the clock to that time. This means that the time will be wrong, but it will at least be showing as later than the shutdown time so timestamps won't suddenly jump back in time.

The file is /etc/fake-hwclock.data and you can read the contents with the cat command

Code: Select all

pi@raspi3 ~ $ cat /etc/fake-hwclock.data 
2017-10-22 04:17:01

pcmanbob
Posts: 2060
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: python and crontab

Sun Oct 22, 2017 9:33 am

Try this.

add a delay to your cron line so that the pi waits for 1 min before running the python program just to see if it works then.

Code: Select all

@reboot sleep 60 && python /home/pi/monitor/boot_time.py >/tmp/myprog.log 2>/tmp/myprog.err
this might reveal if its some thing that is not ready when you are calling the python program.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help

nunocv
Posts: 11
Joined: Thu Jun 08, 2017 11:35 pm

Re: python and crontab

Sun Oct 22, 2017 3:32 pm

this is owesome, i didn't know that the pi don't have an internal clock, and the fake clock is indeed a great method to try to overcome this problem.
my script was actually working, but it was executed before my raspi (which is connected to the network) updated the time online.
so now i added the delay on crontab

Code: Select all

@reboot sleep 60 && python /home/pi/monitor/boot_time.py >/tmp/boottime.log 2>/tmp/boottime.err
thanks you guys

last question, how do mark this topic as solved?

User avatar
DougieLawson
Posts: 30211
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: python and crontab

Sun Oct 22, 2017 3:48 pm

If you're time critical then create a systemd service sudo nano /etc/systemd/system/python_thing.service

Code: Select all

[Unit]
Description=python thing
DefaultDependencies=no
After=local-fs.target time-sync.target

[Service]
Type=oneshot
ExecStart=/home/pi/monitor/boot_time.py
Edit your boot_time.py program and add

Code: Select all

#!/usr/bin/python
as the first line in the program.

make your python program executable
chmod 755 boot_time.py
no need to use python boot_time.py to run it after that.

Make your service file active
sudo systemctl enable python_thing
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

User avatar
scruss
Posts: 1364
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: python and crontab

Mon Oct 23, 2017 3:44 am

DougieLawson wrote:
Sun Oct 22, 2017 3:48 pm
If you're time critical then create a systemd service …
Better yet, create a user systemd service. Save this as ~/.config/systemd/user/boot_time_log.service:

Code: Select all

[Unit]
Description=boot time log
DefaultDependencies=no
After=local-fs.target time-sync.target

[Service]
Type=oneshot
ExecStart=/home/pi/monitor/boot_time.py

[Install]
WantedBy=default.target
then enable it with:

Code: Select all

systemctl --user enable boot_time_log.service
Look Ma, no sudo! Also no weird root-owned files in your home folder too!
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
DougieLawson
Posts: 30211
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: python and crontab

Mon Oct 23, 2017 10:57 am

Thanks Stewart, I've learned a new trick.

Didn't know about --user.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

User avatar
scruss
Posts: 1364
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: python and crontab

Mon Oct 23, 2017 3:26 pm

Thought you might like that, Dougie. systemd keeps on giving!
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

nunocv
Posts: 11
Joined: Thu Jun 08, 2017 11:35 pm

Re: python and crontab

Wed Nov 01, 2017 2:56 am

you're blowing my mind scruss, this looks great
but when i tryed to enabled it, i receve this error

Code: Select all

Failed to execute operation: No such file or directory
to directory you mention does not exist (there was no directory systemd inside ~/.config
so i manually created it
can you give me a step by step?

User avatar
scruss
Posts: 1364
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: python and crontab

Wed Nov 01, 2017 3:42 am

sure:

Code: Select all

mkdir -p ~/.config/systemd/user
then create the ~/monitor/boot_time.py file and make it executable
now save the service file and enable it like I explained.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 40 guests