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.
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 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)
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'm going to run a python script with a parameter using cronjob.
The parameter is password of database that needs to be used in the script.
#!/usr/bin/python
import MySQLdb
import requests
import json
import os
import sys
param=sys.argv
password=param[1]
db = MySQLdb.connect(host="host.com",port=3306,user="user",passwd=password,db="db")
/etc/crontab:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# * * * * * user-name command to be executed
Crontab -e
*/5 * * * * /home/test/run.py "dbpass'"
which python:
/usr/bin/python
Error in /var/spool/mail
/bin/sh: dbpass': command not found
If I run the script manually, it works fine: python run.py "dbpass'"
Any help would be appreciated.
The string you posted contains unicode characters, it's representation is
u'*/5 * * * *\u2002\u2002/home/test/run.py "dbpass\'"'
# or encoded to utf-8:
b'*/5 * * * *\xe2\x80\x82\xe2\x80\x82/home/test/run.py "dbpass\'"'
\u2002 is EN SPACE
So the 5th argument is actually *\u2002\u2002/home/test/run.py which it seems cron treats like a *, taking the next item as the command.
So just replace those EN SPACE characters with normal spaces then it should work.
Your crontab entry should have the password without quotes:
*/5 * * * * /home/test/run.py dbpass
Here is my script(randombg.py):
#!/usr/bin/env python
# -*- coding: utf8 -*-
import random
import subprocess
import os
BACKGROUND = '/home/david/wallpaper/dell2312'
IGNORE_FILES = ['/home/david/wallpaper/dell2312/.directory']
def enumerate():
global BACKGROUND
file_collections = []
for root, dirs, files in os.walk(BACKGROUND):
for file in files:
file_collections.append(os.path.join(root, file))
return file_collections
def randombg():
select_files = list(set(enumerate())-set(IGNORE_FILES))
subprocess.call(['feh', '--bg-scale', random.choice(select_files)])
def main():
while 1:
randombg()
if __name__ == '__main__':
main()
I have run chmod a+x randombg.py and it worked with python randombg.py .Let's say its path is /path/to/randombg.py. Also, run /path/to/randombg.py worked.
However, when I added it to crontab as below:
1 * * * * /path/to/randombg.py
or
01 * * * * python /path/to/randombg.py
or
01 * * * * /usr/bin/python /path/to/randombg.py
All failed.
I can't figure out. Could anyone explain?
PS: ArchLinux
More infomation
When I run ps aux|grep python, I can't find the randombg.py while sometimes it appears.
Addtional logs from crontab redirect stderr:
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
/home/david/dotfiles/randombg.py: line 9: BACKGROUND: command not found
/home/david/dotfiles/randombg.py: line 10: IGNORE_FILES: command not found
/home/david/dotfiles/randombg.py: line 13: syntax error near unexpected token `('
/home/david/dotfiles/randombg.py: line 13: ` def enumerate():'
Try to change your subprocess.call to
subprocess.call("export DISPLAY=:0; feh --bg-scale " + random.choice(select_files), shell=True)
This should export the DISPLAY variable, as scripts run from crontab do not have access to environmental variables by default.