簡體   English   中英

在MacOS上每小時執行一次Python腳本

[英]Execute Python Script Every Hour on MacOS

目標

我有一個用python編寫的腳本。

  1. 連接數據庫
  2. 插入一些假數據

我的目標是每小時執行一次腳本。


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

我做了一些研究,人們提出了一堆應用程序來做到這一點。

所以我嘗試下載/安裝CronniX

創建任務>設置計划>並運行它。

在此輸入圖像描述

它一直在executing ...


任務直到黎明

除此之外,我還嘗試過任務Till Dawn

創建任務>設置計划>並運行它。

結果

在此輸入圖像描述

似乎沒有任何東西插入我的數據庫,即使它說35次成功執行。 它只是在Xcode窗口中彈出我的database.py .


終奌站

我運行python database.py ,它工作得很好,數據插入。


我在想,這是許可問題,但我已經做到了

sudo chmod a+x database.py


我錯過了什么 ? 有什么更好的方法來實現這一目標?

任何建議/提示將不勝感激!

你也可以使用crontab:

每隔一小時:

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

每一分鍾:

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

要查看您的crontabs:

crontab -l

要查看更多選項:

man crontab

您可以使用crontab,我認為它已經安裝在OSX上了?

打開終端並運行crontab -e 這應該打開一個編輯器。 你可以輸入

@hourly /usr/bin/python database.py

根據Rafael Santos的建議,你可以像這樣使用一個cron:

鍵入: crontab -e並添加以下行:

0 * * * * /path/to/your/script

它會在一小時內運行你的腳本。 或者如果您使用的是Django,您可以使用Celery來做同樣的事情。 (它有一個crontab類型)

花時間運行,總和+ 1h,然后每5s驗證電流,舊時間+ 1h

import datetime
import time
mytime= datetime.datetime.now()
currenttime= str(datetime.datetime.time(mytime))
currenttime = currenttime.split(':')
data = list(currenttime)
data[0] = str(int(data[0])+1) # [0] is the first part of 00:00:00, and +1 is the sum for the hour part
print data
print currenttime
while True:
    mytime= datetime.datetime.now()
    currenttime= str(datetime.datetime.time(mytime))
    currenttime = currenttime.split(':')
    # Note: if want test code, delete the while and time.sleep()
    time.sleep(5)
    if data==currenttime:
        exit(0)

你應該把它添加為函數,因為我的代碼是丑陋的xDD

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM