andermutu
Posts: 1
Joined: Sun Sep 17, 2017 5:26 pm

Crontab not working at reboot

Sun Sep 17, 2017 5:31 pm

Hello everybody:

I am trying to run two Python scripts with Crontab. The problem is that if I try to run them with Crontab every 5 minutes everything works well. But if I try to run them with @reboot none of them work. There is my Crontab file:

It works:

*/5 * * * * python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py
*/5 * * * * sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py

It does not work:

@reboot sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py
@reboot python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py

Any idea or suggestion is welcomed. Thanks in advance. :D

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

Re: Crontab not working at reboot

Sun Sep 17, 2017 8:52 pm

Hi.

It probably that your pi has not reached a suitable point in the boot process to actually run your commands, for example networking may not be ready

Try adding a delay of 60 seconds to each entry like this

Code: Select all

@reboot sleep 60 && sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py
@reboot sleep 60 && python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py

also its not good practice to run sudo like that if you want to run your script at root the use sudo crontab -e to create your cron line in the root cron.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
DougieLawson
Posts: 41900
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Crontab not working at reboot

Sun Sep 17, 2017 9:57 pm

Is this a nasty case of "sudoitis" where the OP assumes everything remotely complex needs to run under sudo?
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Crontab not working at reboot

Sun Sep 17, 2017 10:01 pm

also its not good practice to run sudo like that if you want to run your script at root the use sudo crontab -e to create your cron line in the root cron.
False. OP, please ignore this comment by the previous poster.

A) There is nothing wrong with using sudo in crontab (or anywhere else that you need it to get your work done).

B) Editing the root crontab is a formula for pain and suffering. All sorts of bad things can result, starting with just the general hassle of having to keep track of two files, when one is sufficient.

Incidentally, this advice (to use "sudo crontab -e") is given frequently in this forum, so the previous poster can be forgiven for being taken in.
If this post appears in the wrong forums category, my apologies.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: Crontab not working at reboot

Sun Sep 17, 2017 10:02 pm

DougieLawson wrote:
Sun Sep 17, 2017 9:57 pm
Is this a nasty case of "sudoitis" where the OP assumes everything remotely complex needs to run under sudo?
Quite possibly. Impossible to say w/o seeing the code.
If this post appears in the wrong forums category, my apologies.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Crontab not working at reboot

Sun Sep 17, 2017 11:23 pm

Martin Frezman wrote:
Sun Sep 17, 2017 10:02 pm

Quite possibly. Impossible to say w/o seeing the code.
Depends which users crontab is trying to run the code, the OP doesn't say.

Perhaps their 5 minute code runs as Pi and now they're trying to run as root at reboot?

sudo crontab -e will edit the root crontab and shouldn't need the use of sudo <command> as it's run as root anyway.

I would say to the OP to fully qualify the path of python as well, as the root crontab environment is scarcely populated.

moonfarms
Posts: 6
Joined: Tue Jun 16, 2015 8:39 am

Re: Crontab not working at reboot

Wed Dec 13, 2017 1:39 pm

I have same problem, slightly different:
I have two script.py in same directory: /home/pi, same permission
-rwxr-xr-x 1 pi pi 1335 Dec 13 09:55 pws.py
-rwxr-xr-x 1 pi pi 937 Nov 19 11:04 bmp180datalogger.py
crontab:
@reboot /home/pi/./bmp180datalogger.py &
@reboot /home/pi/./pws.py &

bmp180datalogger.py works pws.py not
I can run both from terminal
also tried
@reboot python /home/pi/./pws.py &
@reboot python2.7 /home/pi/./pws.py &
@reboot python2.7 /home/pi/pws.py &

all will run from :/etc/rc.local

Any help greatly appreciated

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

Re: Crontab not working at reboot

Wed Dec 13, 2017 8:59 pm

What does pws.py do ?

does it need the gpio or network to be available. ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

moonfarms
Posts: 6
Joined: Tue Jun 16, 2015 8:39 am

Re: Crontab not working at reboot

Thu Dec 14, 2017 10:27 am

thank you for your time, here is the code:

#!/usr/bin/env python

from lxml import html
import requests
import sys
import time
import datetime

FREQUENCY_SECONDS = 300
while True:
page = requests.get('http://192.168.88.18/livedata.htm')
tree = html.fromstring(page.content)
relPressure = tree.xpath('//input[@name="RelPress"]')[0].value
outTemp = tree.xpath('//input[@name="outTemp"]')[0].value
outHumid = tree.xpath('//input[@name="outHumi"]')[0].value
windDir = tree.xpath('//input[@name="windir"]')[0].value
windSpeed = tree.xpath('//input[@name="avgwind"]')[0].value
windGust = tree.xpath('//input[@name="gustspeed"]')[0].value
solarRadiation = tree.xpath('//input[@name="solarrad"]')[0].value
uv = tree.xpath('//input[@name="uv"]')[0].value
uvi = tree.xpath('//input[@name="uvi"]')[0].value
rainHourly = tree.xpath('//input[@name="rainofhourly"]')[0].value
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M')
f = open("pwsdata.txt","a")
with open("pwsdata.txt", "a") as f:
f.write(st + ",")
f.write(relPressure + ",")
f.write(outTemp + ",")
f.write(outHumid + ",")
f.write(windDir + ",")
f.write(windSpeed + ",")
f.write(windGust + ",")
f.write(solarRadiation + ",")
f.write(uv + ",")
f.write(uvi + ",")
f.write(rainHourly + "\n")
time.sleep(FREQUENCY_SECONDS)
-----------------------------------------------------------------------
basically it is web scraping

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

Re: Crontab not working at reboot

Thu Dec 14, 2017 10:34 am

make a system.service file to do it and wait until the network is up before starting

reboot is too fast [also

Code: Select all

code tags
will help
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

moonfarms
Posts: 6
Joined: Tue Jun 16, 2015 8:39 am

Re: Crontab not working at reboot

Thu Dec 14, 2017 10:36 am

Thank you for your time, will do and get back

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

Re: Crontab not working at reboot

Thu Dec 14, 2017 10:53 am

Or as a quick work around just change your cron lines and add a 60 second delay.

Code: Select all

@reboot sleep 60 && python /home/pi/bmp180datalogger.py 
@reboot sleep 60 && python /home/pi/pws.py
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

moonfarms
Posts: 6
Joined: Tue Jun 16, 2015 8:39 am

Re: Crontab not working at reboot

Thu Dec 14, 2017 11:41 am

@pcmanbob

/home/pi/bmp180datalogger.py was working as is
BUT
@reboot sleep 60 && python /home/pi/pws.py
SOLVED my problem

Thank you very much for your help, I have spent more than one day trying to solve the issue.
Regards, Paul

simonliu
Posts: 2
Joined: Tue Mar 17, 2020 1:43 pm

Re: Crontab not working at reboot

Tue Mar 17, 2020 1:46 pm

pcmanbob wrote:
Sun Sep 17, 2017 8:52 pm
Hi.

It probably that your pi has not reached a suitable point in the boot process to actually run your commands, for example networking may not be ready

Try adding a delay of 60 seconds to each entry like this

Code: Select all

@reboot sleep 60 && sudo python /home/pi/Desktop/Telegram/pyTelegramBotAPI/Correo+.py
@reboot sleep 60 && python /home/pi/Desktop/Telegram/pyTelegramBotAPI/ComandosRes.py

also its not good practice to run sudo like that if you want to run your script at root the use sudo crontab -e to create your cron line in the root cron.
Bravo, adding 'sleep 60' solves my problem~!

AJSG1969
Posts: 49
Joined: Tue Oct 24, 2017 3:24 pm

Re: Crontab not working at reboot

Fri Oct 09, 2020 9:38 am

pcmanbob wrote:
Sun Sep 17, 2017 8:52 pm
It probably that your pi has not reached a suitable point in the boot process to actually run your commands, for example networking may not be ready

Try adding a delay of 60 seconds to each entry like this
I know this is an old post but thank you for the tip, was going crazy trying to figure out why @reboot was not working!!!

Andrew.

daBee
Posts: 105
Joined: Fri Mar 06, 2015 8:25 pm
Location: Toronto
Contact: Website

Re: Crontab not working at reboot

Tue Feb 09, 2021 6:04 pm

Even with sleep 60 mine ain't workin.

Code: Select all

@reboot sleep 60 && /usr/bin/kismet >> /home/pi/rfs/_cronlog.txt 2>&1
No entry into the cronlog file as well. Not sure what's happening.

User avatar
thagrol
Posts: 6119
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Crontab not working at reboot

Tue Feb 09, 2021 6:57 pm

daBee wrote:
Tue Feb 09, 2021 6:04 pm
Even with sleep 60 mine ain't workin.

Code: Select all

@reboot sleep 60 && /usr/bin/kismet >> /home/pi/rfs/_cronlog.txt 2>&1
No entry into the cronlog file as well. Not sure what's happening.
This kismet? https://www.kismetwireless.net/

Giving you the benefit of the doubt that you're not doing anything nefarious with it...
  1. How do you know it's not working?
  2. Have you checked the process list?

    Code: Select all

    ps aux|grep kismet
  3. Does kismet actually produce any output to stdout when running correctly? If it doesn't, the log file will be empty.
  4. The sleep may be failing. That won't show in the log file.
    Try /bin/sleep instead of just sleep.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
neilgl
Posts: 3668
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Crontab not working at reboot

Tue Feb 09, 2021 6:57 pm

Ah, kismet is a GUI program so you need to start it after desktop etc. has loaded. Edit the autostart:

Code: Select all

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
then add your command e.g. for my python timer GUI:

Code: Select all

@python3 /home/pi/scripts/tktimer4.py

daBee
Posts: 105
Joined: Fri Mar 06, 2015 8:25 pm
Location: Toronto
Contact: Website

Re: Crontab not working at reboot

Tue Feb 09, 2021 9:56 pm

Well, I reboot it and there's nothing in the _cronlog.txt file, nor are there any new log files that archive what it's found. The unit works when I induce it manually.

But having said that, the processes:

Code: Select all

root       580 99.9  0.3   5824  3404 ?        R    13:01 228:16 /usr/bin/kismet_client
pi         730  0.0  0.0   4784   500 pts/0    S+   16:49   0:00 grep kismet
Seems root is at 100% core speed, but not logging.

I don't have any desktop loaded on this. There's no file here: /etc/xdg/lxsession/LXDE-pi/autostart

There is a directory:

Code: Select all

[Tue Feb 09 16:55 pi@pi /etc/xdg] tree
.
|-- autostart
|   `-- xdg-user-dirs.desktop
|-- systemd
|   `-- user -> ../../systemd/user
|-- user-dirs.conf
`-- user-dirs.defaults
Here's the OS info:

Code: Select all

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

User avatar
neilgl
Posts: 3668
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Crontab not working at reboot

Tue Feb 09, 2021 10:35 pm

Hold on, that is kismet_client
Let’s rewind - what are you trying to achieve on reboot?

daBee
Posts: 105
Joined: Fri Mar 06, 2015 8:25 pm
Location: Toronto
Contact: Website

Re: Crontab not working at reboot

Tue Feb 09, 2021 10:42 pm

Ya I'm currently reading up on kismet_server.

User avatar
neilgl
Posts: 3668
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Crontab not working at reboot

Tue Feb 09, 2021 10:55 pm

And not forgetting "You will not be able to capture from the built-in Wi-Fi on the Raspberry Pi 3 or Pi 4 unless you also install the nexmon driver patch. "

daBee
Posts: 105
Joined: Fri Mar 06, 2015 8:25 pm
Location: Toronto
Contact: Website

Re: Crontab not working at reboot

Tue Feb 09, 2021 11:03 pm

I'm on RP2. Testing kismet_server, which seems to be working.

User avatar
thagrol
Posts: 6119
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Crontab not working at reboot

Wed Feb 10, 2021 12:32 am

I'm now thoroughly confused.

Assuming that comand you're calling in your crontab is correct and exists it's most likely down to the differences in environment between the logged in session and cron.

Try this in your crontab:

Code: Select all

@reboot /bin/sleep 60; sudo -i -u pi "/usr/bin/kismet >>/home/pi/rfs/_cronlog.txt 2>&1"
Replace pi with the user you want to use. Yes, use the above even in root's crontab (though you'd use -u root in place of -u pi).

Key changes:
  • ";" rather than "&&" means the second command will be run regardless of whether the sleep fails or succeeds. With "&&" it only gets run if the sleep succceeds.
  • "sudo -i -u pi" gets you a much closer environment to the one you get after login.
  • Quoting the kismet command and the redirect should ensure what you get redirected is the output and errors from kismet not from sudo.
  • Removed a " " after ">>". Shouldn't have caused problems but you never know.
Is the sleep 60 there to try to ensure certain services are up before you run /usr/bin/kismet? If so you'll likely have more success using a systemd service with apropriate dependencies.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
neilgl
Posts: 3668
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Crontab not working at reboot

Wed Feb 10, 2021 12:38 am

Pi2 with a usb WiFi dongle?

Return to “Beginners”