crontab 在重新启动时无法运行 python 脚本 [英] crontab failed to run python script at reboot

查看:35
本文介绍了crontab 在重新启动时无法运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 python 脚本的树莓派零

I have a raspberry pi zero with a python script located in

/home/pi/Documents/camProject

将日期和时间写入 log.txt 文件.

that writes the date and time to a log.txt file.

import datetime
import json
now = datetime.datetime.now()
now = str(now)

with open('log.txt','w') as f:
    json.dump(now, f)
    
print('script complete')
print(now)

当我站在 camProject 文件夹中时,我可以从控制台调用和执行脚本.

I can call and execute the script from the console when I am standing in camProject folder.

pi@raspberrypi:~/Documents/camProject $ sudo python3 "/home/pi/Documents/camProject/test.py"
script complete
2020-10-17 08:39:46.238224

我希望这个 test.py 脚本在每次重新启动时运行,所以我从命令控制台执行了

I want this test.py script to run on every reboot, so from the command console I did

sudo crontab -e

在我写的 crontab 脚本的底部

In the bottom of the crontab script I wrote

@reboot sudo python3 "/home/pi/Documents/camProject/test.py"

重新启动树莓派设备后,没有任何反应,日期也没有写入 log.txt 文件.我试过执行

Upon reboot the raspberry pi device nothing happened and the date is not written into the log.txt file. I have tried executing

sudo python3 "/home/pi/Documents/camProject/test.py"

从我的主目录

/home/pi

我在控制台中看到我的 test.py 的打印输出,但 log.txt 没有更新.但是,如果我从 test.py 所在的文件夹执行相同的脚本,一切正常.然后我检查了权限并在我的 camProject 文件夹中

and I see the print out from my test.py in the console but the log.txt is not updated. However, if I execute the same script from the folder where test.py is located, everything works fine. I then checked the permission and in my camProject folder

pi@raspberrypi:~/Documents/camProject $ ls -l
totalt 20
-rwxrwxrwx 1 pi pi   66 okt 17 00:00 camVision.py
-rwxrwxrwx 1 pi pi   28 okt 17 08:50 log.txt
-rwxrwxrwx 1 pi pi  167 okt 17 08:33 test.py
-rwxrwxrwx 1 pi pi  115 okt 17 07:45 test.pyc
drwxrwxrwx 2 pi pi 4096 okt 16 14:50 Video

我猜这个问题与 log.txt is not writeable 有关,当 test.py 从命令控制台执行时,执行发生在 camProject 文件夹之外,因此也不是由 crontab 执行的.我不知道如何解决这个问题?

I guess the problem is related to log.txt is not writeable when test.py is executed from command console when the execution happened to be outside the camProject folder and therefore not by crontab either. I don't know how to fix this problem?

推荐答案

您的程序在当前工作目录中创建文件.cron 作业在调用用户的主目录中运行;因此,您的 cron 作业将文件写入 root 的主目录(在基于 Debian 的平台上可能是 /root).

Your program creates the file in the current working directory. cron jobs run in the invoking user's home directory; thus your cron job writes the file in the home directory of root (probaby /root on Debian-based platforms).

一旦您以 root 身份创建文件,它只能由 root 写入(除非您专门设置权限使其可全局写入,或​​将写入权限分配给特定用户组)

Once you create a file as root, it is only writable by root (unless you specifically set permissions to make it world-writable, or assign write access to a specific user group)

可能将您的脚本更改为写入 /home/pi/log.txt (如果您想要该文件)并确保该文件已经存在,或者可能切换到 如果您以 root 身份运行,请先创建 pi 用户.(一旦文件以正确的所有者和权限存在,root 可以附加到它而不更改所有者或权限.)

Probably change your script to write to /home/pi/log.txt (if that's where you want the file) and make sure the file already exists, or maybe switch to the pi user before creating it if you are running as root. (Once the file exists with the correct owner and permissions, root can append to it without changing the owner or permissions.)

切线,没有必要在 cron 作业中使用 sudo 已经以完全 root 权限运行.

Tangentially, there is no need to use sudo in a cron Job which is already running with full root privileges.

这篇关于crontab 在重新启动时无法运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆