问题
I'm trying to get cron to run this command every 10 minutes;
(In /home/pi/myst-myst/ DIR)
python myst.py `./monitor.sh`
I've tried pretty much everything to get it to work but cron won't execute it properly. Here is what I have at the moment;
*/1 * * * * /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
Any help would be much appreciated.
Is there an alternative to crontab I could use? Could I use a bash script to execute python and then use a cron for that bash script?
回答1:
I've had problems calling both python and perl directly from cron. For perl it boiled down to LIBPATH defaulting to something insufficient.
I'd suggest wrapping your commands in a shell script and adding "set -x" to trace through the problem
#!/bin/sh
set -x
export PYTHONPATH=/my/python/modules:$PYTHONPATH
/usr/bin/python /home/pi/myst-myst/myst.py $(/home/pi/myst-myst/monitor.sh)
Call it directly to make sure it works, and then try calling via cron. Make sure to redirect both stdout and stderr to capture any error messages
*/10 * * * * /home/pi/myscript.sh > /home/pi/stdout 2> /home/pi/stderr
回答2:
You could do something like
*/10 * * * * cd /home/pi/myst-myst/;/usr/bin/python /home/pi/myst-myst/myst.py $(./monitor.sh)
to change working directory before running the command.
Edit: replaced backticks
回答3:
Does your script rely on any environment variables, such as PYTHONPATH
?
If so, the environment will be missing when invoked by cron.
You can try:
*/1 * * * * PYTHONPATH=/my/python/modules/ /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
回答4:
Try this way:
*/1 * * * * /home/pi/myst-myst/myst.py `./monitor.sh`
And add the following in myst.py
#!/usr/bin/env python
来源:https://stackoverflow.com/questions/15334052/crontab-issues-running-python