通过 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版本,支持代码提示!