Python script not executed from crontab - python

I am not able to execute the below attached python script via crontab. When I run script directly from CLI it works just fine, I see just a quick action how browser is opened, everything is setup and then browser closes. But when run in crontab, nothing happens.
Crontab entry:
* * * * * /full/path/to/script.py
I also tried :
* * * * * /usr/bib/python /full/path/to/script.py
SCRIPT:
#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
#open website
browser.get("http:website.php")
# find element in dropdown
Select(browser.find_element_by_name("ma")).select_by_value("817")
# confirm by clicking button
element = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[3]/input")
element.click()
# select dropdown
Select(browser.find_element_by_name("status")).select_by_value("11")
#confirm dropdown
submit = browser.find_element_by_xpath("/html/body/form/table[3]/tbody/tr/td/input")
submit.click()
browser.close()
/var/log/syslog:
May 4 11:34:01 pavol-Vostro-15-3568 CRON[11685]: (root) CMD (/full/path/to/script.py)

I faced a problem similar to yours. Your job might be failing because it requires an X session since you're trying to open a web browser. You should put export DISPLAY=:0; after the schedule in your cronjob, as in
* * * * * export DISPLAY=:0; /usr/bib/python /full/path/to/script.py
If this doesn't work, you could try replacing :0 with the output of echo $DISPLAY in a graphical terminal.

Related

Cron SyntaxError with Selenium Python Script

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)

Running python script with a parameter using cronjob gives error: /bin/sh: password: command not found

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

Py program gives an error if I dont open it with interactive shell

I 've a porgram called searchMachine.py that if I run it straight from IDLE (F5..)it works just fine, but If I run it by just clicking on searchMachine.py it works untill somepoint and then I get an error. See below.
Thanks for any input you could give me.
By the way, any idea how I can make this chrome work in silent? for example minimized?
from selenium import webdriver
from selenium.webdriver.support.ui import Select
print('Please type the machine name.')
machinename = input()
print('')
print('Thank you.')
print('')
print('Results will show below.')
print('')
path_to_chromedriver = 'C:\python34\chromedriver\chromedriver.exe' # change path as needed
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://app.corp'
browser.get(url)
browser.find_element_by_xpath("/html/body/div[4]/form/table[1]/tbody/tr/td[5]/input[1]").send_keys(machinename)
browser.find_element_by_css_selector('input[type=\"submit\"]').click() # working to press submit
xpath = '/html/body/div[4]/form/center[2]/table/thead'
for i in browser.find_elements_by_xpath(xpath):
print (i.text)
xpath = '/html/body/div[4]/form/center[2]/table/tbody/tr'
for i in browser.find_elements_by_xpath(xpath):
print (i.text)
browser.close()
import os
os.system("c:/code/close_chrome_driver.bat")
error is:
The error occurs because the unicode character ▾ or U+25BE BLACK DOWN-POINTING SMALL TRIANGLE cannot be written into Windows console window, as it does not have a match in the CP850 character set as used by your Windows console.
Now, Python croaks loudly when it cannot print the correct value. You could set the PYTHONIOENCODING environment variable to say cp850:ignore which would make the Python process ignore errors; or you can print to stderr instead of stdout, which would use the backslashescape error handling.

Cannot get #reboot Cron job to run Python script

I have a cron job that loads a Python script on reboot but it will just not work.
I have checked the Python script and that works fine from CLi.
The .py basically loads a browser to Google and then sends it to full screen.
(It actually loads another website and enters login details also, but removed for obvious reasons)
Been at this for weeks now and driving me crazy, any ideas?
Raspberry Pi running Raspbian.
$crontab -e
#reboot DISPLAY=:0 python /prtgboot.py
prtgboot.py
'#'!/usr/bin/env python
import commands
import time
webbrowser = "iceweasel"
pgrepcmd = "pgrep %s " % (webbrowser)
process = commands.getoutput(pgrepcmd)
if process == "":
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
browser = webdriver.Firefox()
actions = ActionChains(browser)
browser.get('http://google.co.uk')
elemFullscreen = browser.find_element_by_tag_name('html')
time.sleep(30)
elemFullscreen.send_keys (Keys.F11)
exit()
else:
exit()
Ok, so Petesh was correct. It was #reboot not working correctly.
Changed the cron to * * * * * so my script runs every minute. Normally bad practice, but already setup script to end if browser already running. Working a treat now.
On a positive note, if the browser crashes it will start again for me :)

Python script failing from crontab

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

Categories