-1

I have in an virtualenvironment a python script that I wantt o run every 2 hours using a cronjob

This is my python script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time



PATH = "/usr/bin/chromedriver"

driver = webdriver.Chrome(PATH)



driver.get("http://bitcoin.works")
driver.maximize_window()
time.sleep(2)
driver.find_element_by_link_text('LOGIN').click()
time.sleep(3)
driver.find_element_by_id("login_form_btc_address").send_keys("MyEmail")
driver.find_element_by_id("login_form_password").send_keys("securePassword")
driver.find_element_by_id("login_button").click()
time.sleep(4)
driver.find_element_by_class_name("pushpad_deny_button").click()
time.sleep(7)
driver.find_element_by_id("free_play_form_button").click()
time.sleep(3)

driver.quit()

I also created a file in my folder named run.sh

that has following content

#!/bin/sh

python run.py

I than did

chmod a+x run.sh

After that i edited my the file under crontab -e

to this

0 */2 * * *  cd Documents/scraping && /bin/bash/ run.sh

why do I need the cd Documents/scraping && /bin/bash/ run.sh line?

3 Answers3

0

Make sure your bash script has executable rights:

chmod +x run.sh

Type in crontab -e

and then type the following command:

0 */2 * * * cd /location/of/folder && /bin/bash run.sh
# Dont forget to leave a blank line at the end of the file

It is important to change the directory to the place where your file exists because the commands inside your run.sh are not absolute paths.

Swetank Poddar
  • 1,257
  • 9
  • 23
  • my python files isnt saved in /bin/bash- my python file is in Documents/scrapping/run.py – charlie brown May 10 '20 at 11:58
  • i thought i need to run the .sh file? – charlie brown May 10 '20 at 12:00
  • @charliebrown: I realized later that you have `run.py` AND `run.sh`. Try out the steps I have mentioned above :) – Swetank Poddar May 10 '20 at 12:03
  • In crontab -e I put this line inside 0 */2 * * * cd Documents/scrapping && /bin/bash/ run.sh Is this enough to make it run- also is the content of my sh file correct? – charlie brown May 10 '20 at 12:06
  • You can verify if your `run.sh` executes properly or not by simply executing it from your terminal. Go on your terminal, change your directory to the location of `run.sh` and then type `./run.sh`, if this works then your cronjob should execute properly. – Swetank Poddar May 10 '20 at 12:08
  • ok i ran run.sh but with my virtualenv activated and it works- do I only need to run ./run.sh once and than every 2 hours it should repeat the script – charlie brown May 10 '20 at 12:13
  • I also got this error when running ./run.sh ./run.sh: 4: ./run.sh: deactivate: not found- so if i just leave my virtualenvironment open i dont even need to specify in my sh file to deactivate the virtualenv? – charlie brown May 10 '20 at 12:15
  • Read this answer: https://stackoverflow.com/questions/3287038/cron-and-virtualenv it will explain you how to interact with virtualenv and cron correctly. – Swetank Poddar May 10 '20 at 12:17
  • this answer doesnt make 0 sense – charlie brown May 10 '20 at 12:21
  • Urm, you can remove the deactivate part. It should work fine once you remove that :) – Swetank Poddar May 10 '20 at 12:24
  • Please look at my question again- i edited to what im doing now – charlie brown May 10 '20 at 12:29
  • Now when running ./run.sh I get this error ./run.sh: 2: ./run.sh: source: not found ./run.sh: 3: cd: can't cd to Documents/scrapping However the thing seems to work What happens if i dactivate the virtualenv? Im sure its not working again cause it cant cd into the directory from bash script and - so i think the script will only work if the virtualenv stays activated? – charlie brown May 10 '20 at 12:32
  • the whole thing is now screwed up- cronjobs is such a garbage- if im in virtualenv and I run ./run.sh it works so why do i need to put this source Documents/scrapping/scrape/bin/activate into my bashfile if its not working? Whats the bash file even for? how come there isnt a better way of running a simple file every 2 hours- – charlie brown May 10 '20 at 12:38
  • i removed everything from my bash file except python run.py and it works without errors- so i just leave the virtualenv running no? – charlie brown May 10 '20 at 12:43
  • Haha, yeah they are a pain sometimes... If your script is running properly without the virtual env then it shouldn't be a problem. It should work properly when cron tries to execute the job. – Swetank Poddar May 10 '20 at 12:45
  • can you go inside `Documents/scrapping/scrape`, type pwd and show me the output? – Swetank Poddar May 10 '20 at 12:47
  • /home/ether/Documents/scrapping/scrape --- i need to run it in virtualenv – charlie brown May 10 '20 at 12:52
  • scrape is the name of my virtualenv – charlie brown May 10 '20 at 12:54
0

You can add this line to your crontab:

0 */2 * * *  python <path>/run.py

You can do this, among other ways, with:

crontab -e
0

Try the following command in the cronjob

0 */2 * * *   /bin/bash/ ~/Documents/scrapping/run.sh

I think this may help

manav014
  • 68
  • 1