使用crontab中的参数运行python脚本(来自python中的argparse) [英] Run a python script from with arguments (from argparse in python) from crontab

查看:302
本文介绍了使用crontab中的参数运行python脚本(来自python中的argparse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 argparse 并接受一些参数并从cron运行它的python脚本

I have a python script which uses argparse and accepts a few arguments and run it from cron

示例: python test.py-一个苹果--b球

这需要从crontab计划。可以手动运行它,但cron无法识别参数。请提出解决方案。

This needs to be scheduled from crontab .I can run it manually but cron fails to recognise the arguments .Please suggest solution.

cron作业行如下:

The cron job line looks like :

* * * * * /pathtopython/python test.py --a apple --b ball > /tmp/abc.out 2>&1 


推荐答案

crontest.py 文件代码:

导入argparse

import argparse

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。

Important thing : dont forgot to restart cron in ubuntu.

sudo /etc/init.d/cron restart 

如果您使用其他操作系统,请检查resta的相关命令rt cron。

If you are using different os check for relevant command to restart cron.

这篇关于使用crontab中的参数运行python脚本(来自python中的argparse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆