简体   繁体   中英

Crontab Issues running Python

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?

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

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

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`

Try this way:

 */1 * * * * /home/pi/myst-myst/myst.py `./monitor.sh`

And add the following in myst.py

#!/usr/bin/env python

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