问题
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