应该在启动时运行的 3 个 python 程序中有一个没有,使用 crontab



以下脚本和配置都在运行buster的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()

(注意:我已经审查了一些部分,如我的电子邮件、密码和iftt密钥,以防止任何不必要的恶作剧(;((另一个注意:除了"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>&1

crontab文件打开三个不同的.sh文件(如下所示(,它们都配置有"chmod 755[name].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 /

(注意:我之所以推迟时间,是因为有人猜测,当时并非树莓派上的所有服务都已启动(

我没有收到错误消息,程序似乎没有运行——我知道这一点是因为我查看了每个日志文件的输出(https://i.stack.imgur.com/znqJh.png),正如您所看到的,这两个脚本都有效,但emailCheck没有。

这些程序不在crontab中时都能工作。

还有一点值得注意的是,在重新格式化sd卡后,程序似乎可以工作(尽管这是通过rc.local实现的(,所以我开始怀疑sd卡有某种退化——但因为我对树莓pi知之甚少,我认为这可能是最好的地方。

欢迎所有和任何解决方案,

马修。

已解决!

程序正在运行,判断它是否正在运行的唯一方法是打印到shell——我没有查看shell。解决方案是使用(import-logger(将日志写入一个文件,这意味着我可以检查一个文件来查看脚本是否正在运行。

相关内容

最新更新