I have a script written in python.
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.
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
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.
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 !
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