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

查看:77
本文介绍了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不可写有关,当从命令控制台执行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 (如果您要在其中存储文件),并确保文件已经存在,或者切换到pi 用户,如果您以 root 身份运行,请先创建该用户.(一旦文件具有正确的所有者和权限, 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.)

可能,不必在已经具有完全 root 特权的情况下运行的 cron 作业中使用 sudo .

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天全站免登陆