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
Related
FILE_NAME = 'last_seen_id.txt'
I'm using Ubuntu 18.04. My script use that file and I can only execute it from terminal when I'm in the same directory.
So p.e python3 Documentos/my_bot/my_twitter_bot.py won't work from /home.
So I assume that */5 * * * * python3 /home/david/Documentos/my_bot/my_twitter_bot.py in my crontab won't work either.
What should I do? Pass the file as a parameter? Is there other way?
Thanks
Option 1: use the absolute path to file in your script.
Option 2: make cron first go to the desired directory, and then run the script
*/5 * * * * cd home/david/Documentos/my_bot && python3 my_twitter_bot.py
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 that I am trying to setup as a cron job, but it refuses to run. It does run when I run it by itself calling it as:
python script.py
I have tried setting my evironment variables in the crontab, but I cant get it to work. My crontab looks like this:
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command
SHELL=/bin/bash
PATH=/home/netadmin/bin:/home/net/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/b$
*/2 * * * * PYTHONPATH=/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
Any Ideas on this?
You are mixing up two unrelated concepts:
the Python interpreter location, which is the path to the Python interpreter program (an executable file somewhere)
and the PYTHONPATH, which is a string indicating search locations (directories) for Python libraries. It is not the location of the Python interpreter, but rater a :-separated list of directories. If you don't know what it's useful for, don't use it!
If doing python script.py works, there is generally no need to tweak the PYTHONPATH. You can obtain the full path to the Python interpreter with which:
$ which python
/usr/bin/python
This will print the absolute path to the Python interpreter that you can use in your crontab:
*/2 * * * * /usr/bin/python /path/to/script.py >>/home/net/out.txt 2>&1
Don't tweak PYTHONPATH if you don't need to. If script.py relies on libraries that are not installed on the system, I encourage you to learn & use virtualenvs. It's easy and solves most Python library dependency issues.
You can create a shell script (we'll call it foo.sh for this example) which would look like this:
#! /bin/bash
/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
You need to make foo.sh executable, so you will need to run the following to do that:
chmod +x /home/net/path-to-script/foo.sh
Finally, you can add the shell script to a cron job by running this (which you seem familiar with):
crontab -e
Add a line as follows:
*/2 * * * * /home/net/path-to-script/foo.sh
That should do it, good luck!
Why are you setting environment variable PYTHONPATH, you can run it directly also I believe path for python would be usr instead of user try this
*/2 * * * * cd /home/net/path-to-script ; /usr/bin/python script.py >>/home/net/out.txt 2>&1
This line is incorrect, remove PYTHONPATH
*/2 * * * * PYTHONPATH=/usr/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
=>
*/2 * * * * /usr/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
It's recommended to use Shebang however.
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
I'm trying to run File.py script on Windows with module python cron tab. I have this code and it does not work.
tab = CronTab(tab="""
*/1 * * * * C:\Python27\python.exe C:\Python27\File.py
""")
cmd = 'C:\Python27\python.exe C:\Python27\File.py'
# You can even set a comment for this command
cron_job = tab.new(cmd)
cron_job.minute.every(1)
Can anyone correct please my example or how can I call this File.py correctly
python-crontab only works on Linux/Unix machines because windows doesn't have a crontab service component. It is possible to edit crontab files on windows, but it doesn't provide any scheduling services itself.
You should look for a python scheduler service which will continue to run in the background.