简体   繁体   中英

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

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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM