Run a python script from with arguments (from argparse in python) from crontab

懵懂的女人 提交于 2020-07-20 05:49:26

问题


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

example: python test.py --a apple --b ball

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

The cron job line looks like :

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

回答1:


crontest.py file code :

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 command :

*/1 * * * * python /var/www/html/crontest.py --a apple --b ballon

Important thing : dont forgot to restart cron in ubuntu.

sudo /etc/init.d/cron restart 

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



来源:https://stackoverflow.com/questions/59175088/run-a-python-script-from-with-arguments-from-argparse-in-python-from-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!