I have a python file that creates json files from database, when I run it from the terminal (python pythonfile.py) it works without problems, but when I create a crontab that executes that python every 2 minutes, it does not create the files, the crontab runs the python without problems, I know because before creating the files json makes a change in the database and I can see that change in the database, what will be the problem?
-rwxrwxrwx 1 pi pi 5721 nov 15 02:36 searchProgramData.py
#reboot python /home/pi/aufen/searchProgramData.py >> /tmp/log.txt
*/2 * * * * python /home/pi/aufen/searchProgramData.py >> /tmp/log.txt
query = "SELECT * FROM det_programacion where programacion_id = %s "%programacion_grupo[0][2]
detalles_programacion = run_query(query)
json_str = json.dumps(detalles_programacion,default=datetime_handler)
#escribir json de array
with open('datos.json', 'w') as file:
json.dump(json_str, file)
Related
I'm a beginner trying to write a small Python script that I will run through Cron, but I'm getting a Syntax Error from my Cron output log about the program, although the script works fine running through Terminal.
File "/home/pi/Selenium.py", line 19
clickinput = driver.find_element_by_link_text(f"{date}- File")
^
SyntaxError: invalid syntax
I tried looking up how to do this, and one person with a similar issue changed Cron to run as Bash instead of sh, but when I added SHELL = /bin/bash to the Cron file, nothing happened, and I got the same error. Additionally, when I change the f-string into a non f-string, a "selenium module not found" error gets thrown instead.
Any help figuring out the cause of the error would be appreciated!
Cron command:
PATH = /home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/s$
SHELL = /bin/bash
20 18 * * * /home/pi/Selenium.py > /home/pi/Desktop/backdown.log 2>&1
20 18 * * * env > /home/pi/Desktop/env.output 2>&1
Here is my Python script (minus private info)
#!/usr/bin/env python
import time
from datetime import datetime
from selenium import webdriver
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.get("website");
searchinput = driver.find_element_by_name('mail')
searchinput.send_keys("email")
searchinput = driver.find_element_by_name("pass")
searchinput.send_keys("password")
searchinput.submit()
date = datetime.today().strftime("%m/%d")
if date.find("0") == 0:
date = date.lstrip("0")
clickinput = driver.find_element_by_link_text(f"{date}- File")
clickinput.click()
(This is my first time posting, so sorry if it's bad)
I would like to use a cron task in order to delete media files if the condition is True.
Users generate export files stored in the Media folder. In order to clean export files in the background, I have a Cron task which loops over each file and looks if the expiry delay is passed or not.
I used django-cron library
Example:
File in Media Folder : Final_Products___2019-04-01_17:50:43.487845.xlsx
My Cron task looks like this :
class MyCronExportJob(CronJobBase):
""" Cron job which removes expired files at 18:30 """
RUN_AT_TIMES = ['18:30']
schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'app.export_cron_job'
def do(self):
now = datetime.datetime.now()
media_folder = os.listdir(os.path.join(settings.MEDIA_ROOT, 'exports'))
for files in media_folder:
file = os.path.splitext(files.split(settings.EXPORT_TITLE_SEPARATOR, 1)[1])[0]
if datetime.datetime.strptime(file, '%Y-%m-%d_%H:%M:%S.%f') + timedelta(minutes=settings.EXPORT_TOKEN_DELAY) < now:
os.remove(os.path.join(os.path.join(settings.MEDIA_ROOT, 'exports'), files))
# settings.EXPORT_TOKEN_DELAY = 60 * 24
I edited my crontab -e :
30 18 * * * source /home/user/Bureau/Projets/app/venv/bin/activate.csh && python /home/user/Bureau/Projets/app/src/manage.py runcrons --force app.cron.MyCronExportJob
Then I launched service cron restart
But nothing as changed. My file is still there. However, it should be removed because his date is greater than now + settings.EXPORT_TOKEN_DELAY
I'm using Ubuntu to local dev and FreeBSD as a production server environment.
EDIT:
I tried some things but crontab doesn't work for the moment.
1) * * * * * /bin/date >> /home/user/Bureau/Projets/app/cron_output
==> It works, so crontab works
2) I ran : python manage.py runcrons in my console
==> It works
3) I ran this script (cron.sh):
source /home/user/.bashrc
cd /home/user/Bureau/Projets/app
pyenv activate app
python src/manage.py runcrons --force
deactivate
==> It works
4) I ran this crontab line :
35 10 * * * /home/user/Bureau/Projets/app/utility/cron.sh
==> Service restarted at 10h32, I waited until 10h38 : nothing !
I set up a crontab file to execute a python file every minute however it states it executed but no file is generated.
crontest1.py file contains:
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
if __name__ == '__main__':
f = open('/APPS/CronRun/crontest/dummy1.txt','w')
f.write('hello world it is a file')
f.close()
Crontab file:
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: release/10.0.0/etc/crontab 194170 2009-06-14 06:37:19Z brian $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour mday month wday who command
#
* * * * * root /APPS/CronRun/crontest/crontest1.py
11 11 * * * root /usr/bin/find /APPS/* | grep python | grep core | xargs rm
14 14 17 * * root /APPS/CronRun/Report/report.py
The crontab executes but no file appears.
I tried a simple date >> datelog.txt
which works via crontab but the python file does not seem to execute.
The python file works if executed manually from the shell but not via crontab
I have tried explicitly stating: python /APPS/CronRun/crontest/crontest1.py in the crontab file, but this does not work
I have previously added cron jobs that work fine and still execute daily without any issues
e.g
31,01 * * * * root /APPS/CronRun/makelist/list.py
Try the code without :
if __name__ == '__main__':
Like this:
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
f = open('/APPS/CronRun/crontest/dummy1.txt','w')
f.write('hello world it is a file')
f.close()
Maybe " crontest1.py " is executed by crontab as a module - not "main".
If that doesn't help try to update the Python.
Python 3.1.x and lower
may be the reason.
Hi I schedule my script with cron.d, everything work except a function for move some files from a folder to another.
the function is:
def move_imported_file():
all_file=get_file()
for files in all_file:
#print (files)
shutil.move("/mnt/test-file/"+files, "/mnt/test-file/imported/"+files)
my cron.d file is this:
10 12 * * * root cd /usr/local/sbin/import-file/ && ./myscript.py
If i try to run manually the script, the function move all_file, but if I run the cron.d task, nothing happens
There is any possibility to have a log of what the function are doing?
Thanks
get_file:
def get_file():
my_file = []
os.chdir("/mnt/test-file")
files = glob.glob('*.ics')
for file in files:
my_file.append(file)
#print (my_file)
return my_file
Cron needs the correct PATHs:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin
0 15 * * * root cd /usr/local/sbin/import-file/ && ./myscript.py
I've gone through multiple threads, but I still can't seem to find my problem.
I'm building a really simple Twitter bot that I'd like to fire every hour, on the hour with a cron job from a Raspberry Pi. Here's my crontab:
PYTHONPATH=/usr/bin/python
MAILTO=*myemail*
00 * * * * /home/username/directory/my_script.py >> /var/log/cron.log
Then the script:
#! /usr/bin/env python
import sys
from twython import Twython, TwythonError
from pymarkovchain import MarkovChain
#TWITTER ACCESS
apiKey = KEY
apiSecret = SECRET
accessToken = TOKEN
accessKey = KEY
#text to pull
text = open('/home/username/directory/text.txt').read()
#Generate database and frequency table
mc = MarkovChain('/home/username/directory/markov')
mc.generateDatabase(text)
tweet = mc.generateString()
api = Twython(apiKey,apiSecret,accessToken,accessKey)
try:
api.update_status(status=tweet)
except TwythonError as e:
print e
The first thing I checked was all of my referenced files to make sure they were absolute references. Then, I checked to make sure my file paths were correct. I'm really stumped here. Running the script from command line with the full path works as expected. Any thoughts are appreciated.
After trying the suggestions above and reading countless articles, I learned that cron has to run as root, not as the user. I checked the logs and saw that the user calling the script was the owner of the file, not root. So, running chmod a+x my_script.py took care of it.
Thanks for all the suggestions - especially those getting the errors to the correct log file.
To debug better, you might want to redirect stderr:
00 * * * * /home/username/directory/my_script.py >> /tmp/cron.log 2>&1
# or
00 * * * * /home/username/directory/my_script.py >> /tmp/cron.log 2>/tmp/cron-error.log
(I also changed the path there to make sure your cron user has permission to write output.)
Another thing you could try is run the script with Python in cron:
00 * * * * python /home/username/directory/my_script.py >> /tmp/cron.log 2>&1