Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Python Script Every Hour on MacOS

Goal

I have a script written in python.

  1. connect to database
  2. insert some fake data

My goal is execute that script every hour.


database.py

#!/usr/bin/python


import MySQLdb
import random
import requests
import time


db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="root",         # your username
                     passwd="*********",  # your password
                     db="db-local")       # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# The first line is defined for specified vendor
mac = [ 0x00, 0x24, 0x81,
    random.randint(0x00, 0x7f),
    random.randint(0x00, 0xff),
    random.randint(0x00, 0xff) ]

device_mac =  ':'.join(map(lambda x: "%02x" % x, mac))
cpe_mac = '000D6766F2F6'

url = "https://randomuser.me/api/"
data = requests.get(url).json()
firstname = data['results'][0]['user']['name']['first']
lastname = data['results'][0]['user']['name']['last']
email = data['results'][0]['user']['email']
gender = data['results'][0]['user']['gender']

age_range_options = [">15", "15-25", "25-40","40+"]
age_range = random.choice(age_range_options)

ip = '10.10.10.10'
host_name = 'cron.job'
visit_count = 1
created_at = time.strftime('%Y-%m-%d %H:%M:%S')
updated_at = time.strftime('%Y-%m-%d %H:%M:%S')

sql = ('''INSERT INTO visitors (device_mac,cpe_mac,firstname, lastname, email, gender, age_range,ip,host_name,visit_count,created_at, updated_at) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''')

args = (device_mac,cpe_mac, firstname, lastname, email, gender, age_range,ip, host_name,visit_count, created_at, updated_at)
cur.execute(sql,args)
db.commit()


# for record in records:
# print records

db.close()

CronniX

I did some researches, and people suggested a bunch of apps to do that.

So I've tried downloaded/installed CronniX

create a task > set the schedule > and run it.

enter image description here

It kept hanging on executing ...


Task Till Dawn

In addition to that, I've also tried Task Till Dawn, and again

create a task > set the schedule > and run it.

Result

enter image description here

Nothing seem to insert to my database, even it said 35 succesfully executions. All it did was pop up my database.py inside Xcode window.


Terminal

I run python database.py, It works perfectly fine, and data get inserted.


I was thinking that, it was the permission issue, but I already did

sudo chmod a+x database.py


What did I miss ? What's the better way to achieve this ?

Any suggestions / hints will be much appreciated !

like image 334
code-8 Avatar asked Mar 27 '16 16:03

code-8


2 Answers

You can also just use crontab:

Every hour:

crontab 0 * * * * /path/to/script

Every minute:

crontab * * * * * /path/to/script

To see your crontabs:

crontab -l

To see further options:

man crontab
like image 132
patito Avatar answered Sep 25 '22 15:09

patito


You can use crontab and I think it is already installed on OSX?

Open a Terminal and run crontab -e. This should open an Editor. There you can type

@hourly /usr/bin/python database.py

like image 43
Querenker Avatar answered Sep 23 '22 15:09

Querenker