I am getting some really strange behavior when trying to run a python script at start up using an sh script in crontab.
Here is the python script (it controls wifi lights in my house):
#IMPORTS from lifxlan import BLUE, GREEN, LifxLAN #for lights #INITIALIZE THE LAN lan = LifxLAN(6) #LAN Object of all the lights on the LAN from lifx - 6 lights on the system lights = lan.get_devices_by_group('Living Room') #Object of lights on the LAN print('Trying to turn the lights on...') if True: lights.set_power(power = 'on', duration = 10) #turns on the lights for all the lights on the LAN print('If you see this text, the lights should be on')
Here is the sh script:
#!/bin/sh #launcher.sh #navigate to the home directory, then to python directory, then back to home PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games cd / . /home/pi/python/python-virtual-env/lights/bin/activate cd home/pi/Desktop python3 test.py deactivate cd /
Here is the line in
sudo crontab -e:
@reboot sh /home/pi/Desktop/test.sh > /home/pi/Desktop/test_log 2>&1
I know the file is running because the output of
cat Desktop/test_log is:
Trying to turn the lights on... If you see this text, the lights should be on
But the lights don't turn on.
1) cron is running the .sh script
2) shell is launching the python script
3) I am receiving no errors and the logs are printing the indented code chunk, meaning that it executed the line:
lights.set_power(power = 'on', duration = 10) and didn't meet package problems.
4) But the lights don't turn on.
Notably the lights turn on if i do any of:
sudo sh home/pi/Desktop/test.sh
2) if I paste the lines one by one from the test.sh script into the terminal - including activating the virtual environment.
3) if I run the lines of python code one by one in the python shell
So something is making the LiFX code not run when the
test.sh script is launched by crontab.
I got the
PATH variable for the
test.sh script from by activating the virtual environment and typing
echo $PATH. I assume this is correct.
I also did
sudo pip install lifxlan while in the virtual environment to install that module (https://github.com/mclarkk/lifxlan).
I am stumped at this point. I assume something isn't loading in the virtual environment via cron, but there are no errors and everything works if i run the code from within the virtual environment.
Adding extra information after following the instructions from @Ingo. I tried running this using
systemd, and things worked initially, but then stopped after i updated the
- The full path of the script I am trying to run is:
- Script is not executable, but since i call it with
.servicescript i assume it doesn't need to be.
/usr/bin/python3 /home/pi/python/lights/lightsV4.pyat the command line effectively executes the script (does not need
print()statements in the
.pyscript only call text.
- It does not need wifi (its running a PiR sensor that was triggered by the wifi module) but does need
eth0- its plugged in.
- No remote sources, its the only file running.
The .py script requires a number of packages to load:
import RPi.GPIO as GPIO import time from datetime import datetime, timedelta from lifxlan import BLUE, GREEN, LifxLAN import requests import pytz
The script runs on a while loop. As such i changed the
.servicescript such that
Type=simple, rather than
Type = oneshot. Worked fine initially.
I really appreciate the help. Let me know what I could be missing to cause the