我的脚本首先执行两个python脚本,它们将传感器的温度写入文件,然后通过ftp从.sh文件中上传。
由于某种原因,当我手动运行.sh文件时,温度才会更新。当crontab运行脚本时它只上传ftp,但它似乎没有运行python脚本,除了take_pic.py。
ftp.sh:
echo "Run :$(date)" >> python_temp.log
python write_temp.py >> python_temp.log
python temp_to_file.py >> python_temp_to_file.log
#raspistill -o snapshot2.jpg -n -w 1280 -h 720
python takePic.py
sleep 3
HOST=XX #This is the FTP servers host or IP address.
USER=XX #This is the FTP user that has access to the server.
PASS=XX #This is the password for the FTP user.
NOW=$(date +"%c")
# echo beginne upload
touch work
ftp -inv $HOST << EOF
user $USER $PASS
cd /bilder/
put snapshot2.jpg
rename snapshot2.jpg snapshot.jpg
put temp.js
bye
EOF
# echo erfolgreicher upload
temp_to_file.py
import subprocess
import datetime
import sys
output = subprocess.check_output("temper-poll -q -c" , shell=True)
output = output.rstrip()
#write to file
fobj_out = open("temp.js","w")
fobj_out.write('document.write("' + output + '*C ");')
fobj_out.close()
Crontab:
*/5 * * * * /root/ftp.sh
答案 0 :(得分:1)
>> If I run the script manually everything works perfectly
在运行任何命令之前,您可以在/root/ftp.sh中包含.bash_profile。
. /root/.bash_profile
我经常遇到像cron这样的问题。您在配置文件处于活动状态时在shell中测试它们,当cron作业运行时,环境(PATH和其他东西)不可用。
我看了你的两个脚本,第一个只使用OS命令,但第二个脚本调用temper-poll,这可能不适用于cron job。
答案 1 :(得分:0)
您的ftp.sh
脚本中是否有一个shebang,即它是否以您未在此处显示的#!/usr/bin/env bash
行开头?
如果没有,您需要添加它,或者在您的crontab中,您必须运行bash /root/ftp.sh
。否则您的脚本将无法正确执行。
答案 2 :(得分:0)
主要是由于脚本文件权限和脚本文件的所有权而发生此问题。我面临同样的问题。我发现我的脚本所有者不是超级用户e.g. root.
因此,您必须将您的脚本的权限和所有权设置为超级用户。在下面找到。
首先以超级用户身份编辑您的crontab。
[abc@host] crontab -e
并保存crontab :wq!
现在设置脚本
的权限[abc@host] chmod +x script.sh
[abc@host] chown root:root script.sh
现在重新启动你的crontab。
[abc@host] /etc/init.d/crond restart
答案 3 :(得分:0)
python解释器很可能找到你takePic.py脚本。
因为cronjob在用户主目录中执行(例如:/ home / myuser /)。因此,请使用脚本的完整路径:
python /some/path/takePic.py >> /some/path/python_temp.log