Python Forum
Script won't run at boot - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Script won't run at boot (/thread-25152.html)



Script won't run at boot - ebolisa - Mar-21-2020

Hi,

The code below which speaks a wellcome msg if there's an internet connection, runs manually but not at boot through crontab.

Where did I go wrong?
TIA

#!/usr/local/bin/python3

from subprocess import call
from time import sleep

domain = ('www.google.com')

def _call():
    call(['espeak "Welcome John" 2>/dev/null'], shell=True)

def ping(host):
    ret = call(['ping', '-c', '3', '-W', '5', host], stdout=open('/dev/null', 'w'), stderr=open('/dev/null', 'w'))
    return ret

if __name__ == "__main__":
    p = 1
    while p == 1:
      p = ping(domain)
      if(p == 0):
        _call()
      sleep(.3)



RE: Script won't run at boot - Larz60+ - Mar-21-2020

what does your crontab entry look like?


RE: Script won't run at boot - ndc85430 - Mar-21-2020

Impossible to say really. What are the logs for cron saying? I imagine you'll find them under /var/log somewhere.


RE: Script won't run at boot - ebolisa - Mar-21-2020

Line in crontab: "@reboot python3 /home/pi/wellcome.py"

No errors in boot.log, system nor in messages


RE: Script won't run at boot - ndc85430 - Mar-21-2020

What about cron's logs specifically? On my system, they're in /var/log/cron - YMMV.


RE: Script won't run at boot - ebolisa - Mar-21-2020

Cry I don't see that. I'm running linux on a raspberry zero W board.


RE: Script won't run at boot - ebolisa - Mar-21-2020

I guess espeak cannot be called during boot.

This code works better.

#!/usr/bin/env python

import os, sys, socket
from subprocess import call
from time import sleep

booted = "UEFI" if os.path.exists("/sys/firmware/efi") else "BIOS"

def is_connected():
     try:
         socket.create_connection(("192.168.0.1", 80))
         return True
     except OSError:
         pass
     return False

if __name__ == "__main__":
    p = 1
    while p == 1:
       if(booted == 'BIOS' and is_connected() == True):
         p = 0
         call(['espeak "Welcome John" 2>/dev/null'], shell=True)
       sleep(.3)