I have found similar threads regarding this issue and it's always solved by specifying full paths everywhere. I have done that, but it still doesn't work.
I have written a python script which checks the battery charging status of my laptop. I want this to check every minute and notify me if the status is not 'idle'. I've added an extra bit which logs when the script has run; that bit is only to help debug the issue I have with crontab not running the script.
My script is this:
#!/usr/bin/python3
import os
from datetime import datetime
date_time = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
output = os.popen('sudo /usr/bin/tlp-stat -s -c -b | /usr/bin/grep "BAT0/status"').read().split('=')[1].strip()
if not output == 'Idle':
os.system('/usr/bin/notify-send -u critical "Battery not on idle"')
with open('/home/admin/batteryLog.txt','a') as f:
f.write(output + ": " + date_time+"\n")
I added this script to crontab using crontab -e and it looks like this: * * * * * /usr/bin/python3 /home/admin/checkBattery.py
People here have suggested using env -i /bin/bash --noprofile --norc to get the bash environment that the crontab uses. My script runs fine here so I don't know what could be causing it to not run in crontab.
I have run chmod a+x on the python script, I added the shebang #!/usr/bin/python3 just in case it helped. I included full paths everywhere I could, but I don't think the issue is paths because it runs fine in the noprofile environment.
I also read that people suggest using cd in crontab to change directory to the location of the python script, but my script is in the root directory so it can't be useful to do that.
I've run which python3 and it returns /usr/bin/python3 so I'm definitely in the right place there too.
Does anyone know anything else I can try here?