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
Related
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
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
I have a simple Python script on EC2 to write a text to a file:
with open("/home/ec2-user/python/test.txt", "a") as f:
f.write("test\n\n")
That python script is here:
/home/ec2-user/python/write_file.py
When I run that script manually ('python /home/ec2-user/python/write_file.py') - new text is being written to a file ('/home/ec2-user/python/test.txt').
When schedule same script using Cronjob - no data is being added to a file. My Cronjob looks like this:
* * * * * python /home/ec2-user/python/write_file.py
I verify Cronjob is running and suspecting some ENV parameters are not the same during Cronjob execution (or something else is happening). What could be the case and how to fix it in a simple way?
Thanks.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user or env variables or anything. For example, this single line worked:
0 12 * * * python3 /home/ec2-user/example.py
I also posted this here.
On crontab
HOME=/home/ec2-user/
* * * * * python /home/ec2-user/python/write_file.py
alternatively (testing purpose)
* * * * * root /home/ec2-user/python/write_file.py
On your write_file.py add shebang (first line)
#!/usr/bin/env python
BONUS: (feel free to ignore) my first response debug method for cron on minute tasks:
import os
while True:
print os.popen('pgrep -lf python').read()
executed from shell will pop out all python modules called by cron. After 60 secs you know whats going on.
it's bette to specify the absolute path of the python interpreter because the command may not be recognized by the system. Or you can add the shebang line to your script #!pathToPythonInterpreter at the first line of your script
make sure that the user who planning the execution of script have the right to read and write to the file
make sure that you are not using the system corntab /etc/crontab to planify because then you have to specify the user who will execute the command.
I hope that this will help you
Let me start out by saying I'm extremely new to using python and creating a crontab.
Basically have a custom Python environment created on my dreamhost account using this tutorial
http://wiki.dreamhost.com/Python#Automatic_Installation
Running python script is easy after this. Normally I just type this whenever I have a new session.
source /home/emre801/.bashrc
pb switch 2.7.3
Then I just run any python script using this
pb py code.py
The issue is when I try to create a crontab, I have this right now in my crontab.
*/10 * * * * source /home/emre801/.bashrc; pb switch 2.7.3;pb py code.py
This code emails me the following output
/bin/sh: pb: command not found
My question is how can I set it up the source correctly to not give me this error?
Any help is greatly appreciated
Just create a bash script for this:
/home/emre801/run.sh
#!/bin/bash
source /home/emre801/.bashrc
pb switch 2.7.3
pb /full/path/to/your/code.py
make it executable:
$ chmod 755 /home/emre801/run.sh
And add it to the crontab:
*/10 * * * * /home/emre801/run.sh
or
*/10 * * * * bash /home/emre801/run.sh
what happens to my script in python that does not run through crontab every minute.
My script has execute permissions and then calls two other scripts in python.
This is the content of my crontab (#crontab -l):
*/1 * * * * /usr/bin/rsm/samplesMonitor.py
Thank you guys.
Check /var/log/syslog for errors.
DIAGNOSTICS
cron requires that each entry in a crontab end in a
newline character. If the last entry in a crontab is
missing a newline (ie, terminated by EOF), cron will
consider the crontab (at least partially) broken. A
warning will be written to syslog.
Update: According to your log message, the script is running but returning an error code. Cron will email you the output, if you have a mail agent installed.
Try either:
install a mail agent, such as: apt-get install exim4
change your cron line to log to file, like so:
* * * * * /usr/bin/rsm/samplesMonitor.py 2>&1 >> /tmp/script.log
Update 2: I re-read your question and it acurred to me that maybe you are running into python import issues. You say that your script calls two other scripts. My suggestion would be to test running your script from /. Python has a default behavior to find imports in the current working directory, so make sure your script can run from any path location.
In the crontab, you can set the starting working directory by calling your script from within another shell process. For example:
bash -c "cd THE_WORKING_DIR;/usr/bin/rsm/samplesMonitor.py"
I believe it should be */1, not *\1.
It should be */1 instead of *\1 (forward slash instead of backslash). Also, make sure the path is correct; there usually are no subdirectories under /usr/bin.
If you want it to run every minute, just do this
* * * * * /usr/bin/rsm/samplesMonitor.py