python - 使用来自 crontab 的参数(来自 python 中的 argparse)运行 python 脚本
问题描述
我有一个 python 脚本,它使用argparse
并接受一些参数并从 cron 运行它
例子:python test.py --a apple --b ball
这需要从 crontab 安排。我可以手动运行它,但 cron 无法识别参数。请提出解决方案。
cron 作业线如下所示:
* * * * * /pathtopython/python test.py --a apple --b ball > /tmp/abc.out 2>&1
解决方案
crontest.py
文件代码:
导入参数解析
parser = argparse.ArgumentParser()
parser.add_argument('--a', help="First parameter")
parser.add_argument('--b', help="First parameter")
args = parser.parse_args()
file = open('/var/www/html/research/coding-challenge/geek.txt','a')
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file.write(args.a+args.b)
file.close()
cron 命令:
*/1 * * * * python /var/www/html/crontest.py --a apple --b ballon
重要的事情:不要忘记在 ubuntu 中重新启动 cron。
sudo /etc/init.d/cron restart
如果您使用不同的操作系统,请检查相关命令以重新启动 cron。
推荐阅读
- react-native - React Native:错误:捆绑失败:index.js:“”不是有效的标识符名称
- angularjs - 从Angularjs中的数组中获取特定元素
- c# - 外键关系没有出现在 edmx 与 mysql 数据库与 asp mvc 与数据库第一
- angularjs - 在ajax formdata中获取415(不支持的媒体类型)发布到spring
- flutter - 如何在颤动中从 SelectableText 小部件中获取选定的文本
- json - 使用 Spark 2.4 识别空 JSON 文件
- c# - 在 Unity 中捕获 360 度屏幕截图会忽略 UI 层
- sql - 如何将一个 SQL 列转换为同一行的多个列?
- java - 在 Android 中,我如何实现一个全局触摸跟踪服务,即使屏幕上正在运行其他应用程序也能正常工作
- java - Spring Boot 配置问题中的 JWT 令牌