Cant run a selenium python script with crontab - python

I have a python script that finds my grades in a webpage and sends me an email if any of my grades have been changed. Im now trying to run this script every hour with crontab.
The script works when running through terminal:
python3 /home/pi/Desktop/grades/script.py
In my script, I am trying to run crontab with * * * * * python3 /home/pi/Desktop/grades/script.py
I also added the shell to use: SHELL=/bin/bash
When the script is ran through the IDLE, it cannot find the crontab directory to schedule the scipt. I have the PATH variable set, and I searched about this and nothing has worked.

This could be couple of ways to handle this.
1. You must specify full path of python
* * * * * '/usr/bin/python3 /home/pi/Desktop/grades/script.py'
2. You should put shebang line in your script, ref examples.
#!/usr/bin/env python3 or #!/usr/bin/python3.
* * * * * /home/pi/Desktop/grades/script.py

Related

crontab working for one python script but not the other, why?

After a bit of frustration I've managed to run a test.py script from a tutorial I followed.
Works a treat but when I try my own script trending.py it does nothing
both scripts work from Atom like a dream
but trending.py just doesn't execute from crontab
* * * * * /usr/bin/python ~/code/trending.py
* * * * * /usr/bin/python ~/code/test.py
does the contents of the script effect whether crontab with allow it to run?
using late 2013 iMac OS Catalina v.10.15.7 (incase that make a difference)

crontab only works in tmp directory

I am trying to use crontab to run a python script.
Here is what I put into the crontab -e
* * * * * path/to/python /Users/myname/Desktop/script.py
However, it does not run the script unless I put the python script in /tmp/:
* * * * * path/to/python /tmp/script.py
Does anyone know how to make crontab run a python file that is on my Desktop, not in the /tmp directory?
This is my python3 script I am trying to run:
from pync import Notifier
import os
Notifier.notify('notification', group=os.getpid())
Solved:
Since I am on macOS Catalina here is what you do.
Go to system preferences, security & privacy, click the lock to make changes, click full disk access, select the plus, add in cron (located at /usr/sbin/cron) and give it access.
This will fix the issue

Raspberry Pi Crontab Python script needs write permission

I have a python script that I would like to run on my raspberry pi every 15 minutes. The script should do something and then post the reports into a txt file. When I run the script using
sudo ./automate.py
everything works fine. But my crontab never posts a report. I checked the crontab logs, and the script is running; however, it isn't posting anything to the txt files (One of which just appends "Executed at time", so it should post every time).
When I run the script without sudo, I get an error saying I do not have permission to write to a the file, so I assume that is the problem.
I have tried the following in crontab:
*/15 * * * * python /path/automate.py
*/15 * * * * sudo python /path/automate.py
15 * * * * python /path/automate.py
15 * * * * sudo python /path/automate.py
I have created these crontab scripts for both the user (which has total permissions) and the root user (pi) with the same problems.
I also should mention that the files are on an external HDD, but both accounts have write permissions to the drive so I doubt that was the problem.
I needed to change the directory in the crontab command to the working directory of the project. Making it:
cd /path/to/file; python automate.py

crontab Mac running a Python script everyday at the same time

Tried to follow the instructions from this question but it didn't work with me. As background info I can tell you that there's an instance of Python 2.7 installed on Mac by default and my instance is actually python 3.6, hence the Python3. I'm doing something wrong but I can't figure out what. I know that as is it will run every minute, I just want to make it work first and then change the time.
* * * * * cd /User/Users/cannopy/PycharmProjects/untitled python3 /User/Users/cannopy/PycharmProjects/untitled/test.py
EDIT:
After changing some things I managed to make it work with this:
* * * * * cd /Users/cannopy/PycharmProjects/untitled/ && ~/PycharmProjects/untitled/venv/bin/python test.py
Try,
* * * * * cd /User/Users/cannopy/PycharmProjects/untitled && python3 test.py >> cron_result.txt
A crontab entry can only contain a single line. You can combine commands with ; or && but ideally your Python script should not require to run in a particular directory.
If the script requires imports or data files from the directory where it is installed, maybe have it examine sys.argv[0] to discover its location.

Crontab Issues running Python

I'm trying to get cron to run this command every 10 minutes;
(In /home/pi/myst-myst/ DIR)
python myst.py `./monitor.sh`
I've tried pretty much everything to get it to work but cron won't execute it properly. Here is what I have at the moment;
*/1 * * * * /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
Any help would be much appreciated.
Is there an alternative to crontab I could use? Could I use a bash script to execute python and then use a cron for that bash script?
I've had problems calling both python and perl directly from cron. For perl it boiled down to LIBPATH defaulting to something insufficient.
I'd suggest wrapping your commands in a shell script and adding "set -x" to trace through the problem
#!/bin/sh
set -x
export PYTHONPATH=/my/python/modules:$PYTHONPATH
/usr/bin/python /home/pi/myst-myst/myst.py $(/home/pi/myst-myst/monitor.sh)
Call it directly to make sure it works, and then try calling via cron. Make sure to redirect both stdout and stderr to capture any error messages
*/10 * * * * /home/pi/myscript.sh > /home/pi/stdout 2> /home/pi/stderr
You could do something like
*/10 * * * * cd /home/pi/myst-myst/;/usr/bin/python /home/pi/myst-myst/myst.py $(./monitor.sh)
to change working directory before running the command.
Edit: replaced backticks
Does your script rely on any environment variables, such as PYTHONPATH?
If so, the environment will be missing when invoked by cron.
You can try:
*/1 * * * * PYTHONPATH=/my/python/modules/ /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
Try this way:
*/1 * * * * /home/pi/myst-myst/myst.py `./monitor.sh`
And add the following in myst.py
#!/usr/bin/env python

Categories