透過Crontab 執行Python 腳本
問題:使用者在嘗試使用Linux crontab 執行Python 腳本時可能會遇到困難,特別是當旨在每10 分鐘運行一次。各種解決方案(例如修改 anacron 檔案或使用 crontab -e)可能無效,讓使用者質疑是否需要重新啟動特定服務或應編輯設定檔。
答案:
要解決此問題,請參閱以下指南:
- 編輯crontab> 在 在 在>終端機中輸入crontab -e 以存取crontab .
- 新增腳本: 將所需的命令追加到crontab 檔案中,如下所示,每10 分鐘執行一次腳本:
*/10 * * * * /usr/bin/python /home/souza/Documents/Listener/listener.py
- 儲存crontab 檔案: 按Ctrl X 退出,然後按Y 儲存變更。
文件配置:
需要編輯的文件是 crontab 文件,可以使用 crontab -e 命令存取和修改該文件。腳本:
您的 Python 腳本必須正確配置執行所需的操作。作為參考,這裡是提供的腳本,適合每 10 分鐘執行一次:<code class="python">#!/usr/bin/python # -*- coding: iso-8859-15 -*- import json import os import pycurl import sys import cStringIO if __name__ == "__main__": name_server_standart = "Server created by script %d" json_file_standart = """{ "server" : { "name" : "%s", "imageRef" : "%s", "flavorRef" : "%s" } }""" curl_auth_token = pycurl.Curl() gettoken = cStringIO.StringIO() curl_auth_token.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1") curl_auth_token.setopt(pycurl.POST, 1) curl_auth_token.setopt( pycurl.HTTPHEADER, ["X-Auth-User: cpca", "X-Auth-Key: 438ac2d9-689f-4c50-9d00-c2883cfd38d0"], ) curl_auth_token.setopt(pycurl.HEADERFUNCTION, gettoken.write) curl_auth_token.perform() chg = gettoken.getvalue() auth_token = chg[ chg.find("X-Auth-Token: ") + len("X-Auth-Token: ") : chg.find("X-Server-Management-Url:") - 1 ] token = "X-Auth-Token: {0}".format(auth_token) curl_auth_token.close() # ---------------------------- getter = cStringIO.StringIO() curl_hab_image = pycurl.Curl() curl_hab_image.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/images/7") curl_hab_image.setopt(pycurl.HTTPGET, 1) # Removing this line allows the script to run. curl_hab_image.setopt(pycurl.HTTPHEADER, [token]) curl_hab_image.setopt(pycurl.WRITEFUNCTION, getter.write) # curl_list.setopt(pycurl.VERBOSE, 1) curl_hab_image.perform() curl_hab_image.close() getter = cStringIO.StringIO() curl_list = pycurl.Curl() curl_list.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/servers/detail") curl_list.setopt(pycurl.HTTPGET, 1) # Removing this line allows the script to run. curl_list.setopt(pycurl.HTTPHEADER, [token]) curl_list.setopt(pycurl.WRITEFUNCTION, getter.write) # curl_list.setopt(pycurl.VERBOSE, 1) curl_list.perform() curl_list.close() # ---------------------------- resp = getter.getvalue() con = int(resp.count("status")) s = json.loads(resp) lst = [] for i in range(con): lst.append(s["servers"][i]["status"]) for j in range(len(lst)): actual = lst.pop() print actual if actual != "ACTIVE" and actual != "BUILD" and actual != "REBOOT" and actual != "RESIZE": print "Enters the if block." f = file("counter", "r+w") num = 0 for line in f: num = line content = int(num) + 1 ins = str(content) f.seek(0) f.write(ins) f.truncate() f.close() print "Increments the counter." json_file = file("json_file_create_server.json", "r+w") name_server_final = name_server_standart % content path_to_image = "http://192.168.100.241:8774/v1.1/nuvemcpca/images/7" path_to_flavor = "http://192.168.100.241:8774/v1.1/nuvemcpca/flavors/1" new_json_file_content = json_file_standart % ( name_server_final, path_to_image, path_to_flavor, ) json_file.seek(0) json_file.write(new_json_file_content) json_file.truncate() json_file.close() print "Updates the JSON file." fil = file("json_file_create_server.json") siz = os.path.getsize("json_file_create_server.json") cont_size = "Content-Length: %d" % siz cont_type = "Content-Type: application/json" accept = "Accept: application/json" c_create_servers = pycurl.Curl() logger = cStringIO.StringIO() c_create_servers.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/servers") c_create_servers.setopt(pycurl.HTTPHEADER, [token, cont_type, accept, cont_size]) c_create_servers.setopt(pycurl.POST, 1) c_create_servers.setopt(pycurl.INFILE, fil) c_create_servers.setopt(pycurl.INFILESIZE, siz) c_create_servers.setopt(pycurl.WRITEFUNCTION, logger.write) print "Executes the curl command." c_create_servers.perform() print logger.getvalue() c_create_servers.close()</code>
以上是如何透過Crontab執行Python腳本監控伺服器狀態並建立新實例?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本教程演示如何使用Python處理Zipf定律這一統計概念,並展示Python在處理該定律時讀取和排序大型文本文件的效率。 您可能想知道Zipf分佈這個術語是什麼意思。要理解這個術語,我們首先需要定義Zipf定律。別擔心,我會盡量簡化說明。 Zipf定律 Zipf定律簡單來說就是:在一個大型自然語言語料庫中,最頻繁出現的詞的出現頻率大約是第二頻繁詞的兩倍,是第三頻繁詞的三倍,是第四頻繁詞的四倍,以此類推。 讓我們來看一個例子。如果您查看美國英語的Brown語料庫,您會注意到最頻繁出現的詞是“th

本文解釋瞭如何使用美麗的湯庫來解析html。 它詳細介紹了常見方法,例如find(),find_all(),select()和get_text(),以用於數據提取,處理不同的HTML結構和錯誤以及替代方案(SEL)

處理嘈雜的圖像是一個常見的問題,尤其是手機或低分辨率攝像頭照片。 本教程使用OpenCV探索Python中的圖像過濾技術來解決此問題。 圖像過濾:功能強大的工具圖像過濾器

PDF 文件因其跨平台兼容性而廣受歡迎,內容和佈局在不同操作系統、閱讀設備和軟件上保持一致。然而,與 Python 處理純文本文件不同,PDF 文件是二進製文件,結構更複雜,包含字體、顏色和圖像等元素。 幸運的是,借助 Python 的外部模塊,處理 PDF 文件並非難事。本文將使用 PyPDF2 模塊演示如何打開 PDF 文件、打印頁面和提取文本。關於 PDF 文件的創建和編輯,請參考我的另一篇教程。 準備工作 核心在於使用外部模塊 PyPDF2。首先,使用 pip 安裝它: pip 是 P

本教程演示瞭如何利用Redis緩存以提高Python應用程序的性能,特別是在Django框架內。 我們將介紹REDIS安裝,Django配置和性能比較,以突出顯示BENE

本文比較了Tensorflow和Pytorch的深度學習。 它詳細介紹了所涉及的步驟:數據準備,模型構建,培訓,評估和部署。 框架之間的關鍵差異,特別是關於計算刻度的

本教程演示了在Python 3中創建自定義管道數據結構,利用類和操作員超載以增強功能。 管道的靈活性在於它能夠將一系列函數應用於數據集的能力,GE

Python是數據科學和處理的最愛,為高性能計算提供了豐富的生態系統。但是,Python中的並行編程提出了獨特的挑戰。本教程探討了這些挑戰,重點是全球解釋


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!