下面的脚本和配置是在Raspberry Pi B+ (我相信)上运行的。
脚本'check.py‘如下
#!/usr/bin/env/ python3
import time
print('sleeping')
time.sleep(15)
print('HEASPIDHAPOISHDOAISHD')
def emailChecker():
import imaplib
import email
import os
import time
print('initiating emailinfiniteCheck')
EMAIL = 'CENSORED@gmail.com'
PASSWORD = 'CENSORED'
SERVER = 'imap.gmail.com'
FROM = 'CENSORED'
# connect to the server and go to its inbox
mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL, PASSWORD)
while True:
try:
mail.select('inbox')
status, data = mail.search(None, 'ALL')
mail_ids = []
for block in data:
mail_ids += block.split()
for i in mail_ids:
status, data = mail.fetch(i, '(RFC822)')
for response_part in data:
refuse = False
if isinstance(response_part, tuple):
message = email.message_from_bytes(response_part[1])
mail_from = message['from']
mail_subject = message['subject']
mail_date = message['date']
if message.is_multipart():
mail_content = ''
for part in message.get_payload():
if part.get_content_type() == 'text/plain':
mail_content += part.get_payload()
else:
mail_content = message.get_payload()
mail_content = message.get_payload()
position = 0
for i in mail_from:
if i == '<':
change = True
firstPos = position + 1
if i == '>':
change = True
lastPos = position
position = position + 1
mail_from = (mail_from[firstPos:lastPos])
myFile = open('/home/pi/GLaDOS/text-files/read_messages.txt', 'rt')
for i in myFile:
i = i.replace('\n', '')
if i == mail_date:
refuse = True
myFile.close()
if refuse == False:
myFile = open('/home/pi/GLaDOS/text-files/read_messages.txt', 'a')
myFile.write(mail_date)
myFile.close()
myFile = open('/home/pi/GLaDOS/text-files/command.txt', 'wt')
myFile.write(str(mail_subject))
myFile.close()
time.sleep(60)
except Exception as e:
print('error, retrying', e)
print('well I got here...')
emailChecker()(注意:我已经审查了一些部分,如我的电子邮件,密码和ifttt密钥,以防止任何不必要的恶作剧(;)(另一个注意:您可能不需要查看除‘check.py’以外的任何其他程序)
我正在使用crontab运行三个单独的python脚本,每个脚本的输出都通过管道输出到一个日志文件中,这三个python脚本中只有一个与输出一起运行。
这是crontab文件:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot sleep 60 && sh /home/pi/GLaDOS/launcherCheck.sh >/home/pi/logs/check.log 2>&1
@reboot sleep 60 && sh /home/pi/GLaDOS/test.sh >/home/pi/logs/test.log 2>&1
@reboot sleep 60 && sh /home/pi/GLaDOS/launcherGLaDOS.sh >/home/pi/logs/GLaDOS.log 2>&1crontab文件打开三个不同的.sh文件(如下所示),它们都是使用‘chmod755name.sh’配置的。
.sh文件'test.sh‘-
#!/bin/sh
# launcherGLaDOS.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/GLaDOS
sudo python3 test.py
cd /.sh文件'launcherGLaDOS.sh‘-
#!/bin/sh
# launcherGLaDOS.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/GLaDOS
sudo python3 GLaDOS.py
cd /.sh文件'launcherCheck.sh‘-
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/GLaDOS
sudo python3 check.py
cd /(注:我之所以将时间延迟,是因为有人猜测,到那时,并不是所有的覆盆子pi上的服务都已经开始)
我没有收到错误消息,程序似乎没有运行-我之所以知道这一点,是因为我检查了每个日志文件(https://i.stack.imgur.com/znqJh.png)的输出,正如您所看到的,两个脚本都可以工作,但emailCheck没有。
当不在crontab中时,这些程序都可以工作。
另一件值得注意的事情是,在重新格式化sd卡后,程序似乎可以正常工作(尽管是通过rc.local),因此我开始怀疑sd卡是否有某种退化-但因为我对树莓派知之甚少,我认为这可能是最好的去处。
欢迎所有和任何解决方案,
马修。
发布于 2020-08-14 19:13:53
解决了!
程序正在运行,判断它是否正在运行的唯一方法是打印到shell --我没有查看shell。解决方案是使用(import logger)将日志写入一个文件,这意味着我可以只检查一个文件,看看脚本是否正在运行。
https://stackoverflow.com/questions/63064429
复制相似问题