英文:
Crontab not running a Processing script
问题
以下是翻译好的部分:
我有一个从命令行运行的Python脚本,它执行三件事情:
1)终止当前正在运行的所有Processing程序。
2)运行一个新的Processing程序。
3)关闭树莓派。
当我从命令行运行此命令时,它能够无缺地运行。然而,当我使用crontab调用这个Python脚本时,只有第1和第3个步骤能够正确运行。我想知道的是,为什么第2个命令(运行一个新的Processing程序)在我从命令行运行Python脚本时有效,但在使用crontab时却无效?
这是我的Python脚本:
import os # 使用操作系统
import subprocess # 使用子进程调用
from time import sleep # 使用延时
from subprocess import call
os.system('sudo killall java')
sleep(5)
child = subprocess.Popen('sudo processing-java --sketch=/home/pi/Desktop/LaserCannonProcessing/LCshutdown --run', shell=True)
sleep(15)
call("sudo poweroff", shell=True)
这是我的crontab:
50 20 * * * sudo /usr/bin/python3 /home/pi/Desktop/Shutdown.py
有人知道为什么crontab无法成功运行运行Processing程序的命令吗?如果知道,是否有办法修复这个问题,使得crontab能够运行那一行命令?谢谢。
英文:
I have a python script that I am running from the command line that does three things
1.) Kills all Processing programs currently running
2.) Runs a new Processing program
3.) Shut downs Raspberry Pi
When running this command from the command line, it works flawlessly. Yet, when calling this Python script using crontab, only the 1st and 3rd processes run correctly. What I want to know is why the 2nd command (running a new Processing program) works when I run the Python script from the command line, but not from a crontab?
Here is my Python script
import os # Use operating system
import subprocess # Use subprocess calls
from time import sleep # Use sleep
from subprocess import call
os.system('sudo killall java')
sleep(5)
child = subprocess.Popen('sudo processing-java --sketch=/home/pi/Desktop/LaserCannonProcessing/LCshutdown --run', shell=True) #
sleep(15)
call("sudo poweroff", shell = True)
and here is my crontab
50 20 * * * sudo /usr/bin/python3 /home/pi/Desktop/Shutdown.py
Does anyone know why crontab can not successfully run the command to run a processing program? If so, is there any way I can fix this and make crontab run that line? Thanks
答案1
得分: 1
The cron daemon automatically sets several environment variables. The default path is set to PATH=/usr/bin:/bin.
So if the processing-java
command is not present in the cron specified path, you should either use the absolute path to the command or change the cron $PATH variable.
Using shell=True
is masking the problem...
E.g.
In [7]: child = subprocess.Popen('bla', shell=True)
/bin/sh: bla: command not found
In [8]: child
Out[8]: <subprocess.Popen at 0x107ac8c50>
You can add some debugging to your script to find out the real issue:
try-except
around the subprocess call andshell=True
- print the
os.environ["PATH"]
- check permissions on files (if your process needs to read/write to files)
英文:
The cron daemon automatically sets several environment variables. The default path is set to PATH=/usr/bin:/bin.
So if the processing-java
command is not present in the cron specified path, you should either use the absolute path to the command or change the cron $PATH variable.
Using shell=True
is masking the problem...
E.g.
In [7]: child = subprocess.Popen('bla', shell=True)
/bin/sh: bla: command not found
In [8]: child
Out[8]: <subprocess.Popen at 0x107ac8c50>
You can add some debugging to your script to find out the real issue:
try-except
around the subprocess call andshell=True
- print the
os.environ["PATH"]
- check permissions on files (if your process needs to read/write to files)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论