为什么我的 python 脚本在终端和 cron 之间的工作方式不同?

Why does my python script work differently between the teminal and cron?

我正在尝试诊断一些恶作剧,其中我的 python 代码 (Lightning.py) 在终端 window 中执行 "sudo ./Lightning.py" 但不是我的 cron 作业。

我是 linux 的新手,欢迎任何帮助!

这是 Lightning.py

的代码
#!/usr/bin/env python3

import wget
import os
from shutil import copyfile

if os.path.exists("prepy.txt"):
  os.remove("prepy.txt")
print('Beginning file download from Saratoga Weather with wget module')

url = 'http://saratoga-weather.org/USA-blitzortung/placefile.txt'
wget.download(url, '/home/user/Desktop/prepy.txt')

old_lightnings = ['0,1,2,', '0,1,3,','0,1,4,','0,1,5,','0,1,6,','0,1,7,','0,1,8,','0,1,9,']

with open('prepy.txt') as oldfile, open('lightning.txt', 'w') as newfile:
    for line in oldfile:
        if not any(old_lightning in line for old_lightning in old_lightnings):
            newfile.write(line)
copyfile('/home/user/Desktop/lightning.txt', '/home/user/Music/lightning.txt')
print('\nLightning.py has been run')

这是关联的 cron 作业。它每分钟设置一次,所以我可以测试它。

* * * * * /usr/bin/python /home/user/Desktop/Lightning.py

我目前正在 Ubuntu 18.04.3.

中进行测试

唯一发生的事情是,每当 cron 运行s 时,它都会创建一个文件 "prepy(1).txt",但不会对行进行排序和删除并将其保存为正确的输出。

作为额外的奖励,这里是从脚本下载的文件示例:

;Bliztortung USA Placefile for GRLevel3
; Placefile by Ken True, saratoga-weather.org
; Updated: Sun, 24 Nov 2019 08:15:01 PST
RefreshSeconds: 300
IconFile: 1,30,30,15,15,http://saratoga-weather.org/USA-blitzortung/lightningicons.png
Icon: 29.860824,-76.139117,0,1,9,Blitzortung @ 06:15:53 PST
Icon: 35.379265,-71.096417,0,1,9,Blitzortung @ 06:16:09 PST
Icon: 31.609272,-73.765092,0,1,9,Blitzortung @ 06:16:13 PST
Icon: 31.885003,-73.709302,0,1,9,Blitzortung @ 06:16:13 PST
Icon: 35.459427,-70.928328,0,1,9,Blitzortung @ 06:17:28 PST
Icon: 35.552307,-71.060657,0,1,9,Blitzortung @ 06:17:28 PST
Icon: 31.669056,-73.737014,0,1,9,Blitzortung @ 06:19:35 PST
Icon: 31.669056,-73.737014,0,1,9,Blitzortung @ 06:19:35 PST
Icon: 30.22179,-75.510468,0,1,9,Blitzortung @ 06:19:45 PST
Icon: 29.831653,-76.047618,0,1,9,Blitzortung @ 06:20:38 PST
Icon: 29.812147,-76.042173,0,1,9,Blitzortung @ 06:20:38 PST
Icon: 30.149676,-75.405737,0,1,9,Blitzortung @ 06:21:29 PST
Icon: 31.799604,-73.352328,0,1,9,Blitzortung @ 06:23:57 PST
Icon: 31.831979,-73.345653,0,1,9,Blitzortung @ 06:23:57 PST
Icon: 38.715605,-67.371949,0,1,9,Blitzortung @ 06:26:28 PST
Icon: 38.671515,-67.335955,0,1,9,Blitzortung @ 06:26:28 PST
Icon: 30.147951,-75.345644,0,1,9,Blitzortung @ 06:26:51 PST
Icon: 31.878007,-73.527605,0,1,9,Blitzortung @ 06:26:58 PST
Icon: 31.848858,-73.334373,0,1,9,Blitzortung @ 06:26:59 PST
Icon: 29.562893,-76.483871,0,1,9,Blitzortung @ 06:27:24 PST
Icon: 29.640704,-76.541928,0,1,9,Blitzortung @ 06:27:24 PST
Icon: 34.266843,-71.379808,0,1,9,Blitzortung @ 06:27:44 PST
Icon: 29.810437,-75.893204,0,1,9,Blitzortung @ 06:28:00 PST
Icon: 29.878976,-75.931348,0,1,9,Blitzortung @ 06:28:00 PST
Icon: 29.903286,-75.94225,0,1,9,Blitzortung @ 06:28:00 PST
Icon: 18.400364,-105.684513,0,1,9,Blitzortung @ 06:28:11 PST
Icon: 38.524655,-67.036093,0,1,9,Blitzortung @ 06:28:52 PST
Icon: 29.469489,-76.511855,0,1,9,Blitzortung @ 06:29:14 PST
Icon: 29.661606,-76.533699,0,1,9,Blitzortung @ 06:29:14 PST
Icon: 29.661606,-76.533699,0,1,9,Blitzortung @ 06:29:14 PST

附加信息:

这是 .py 文件的简化版本

#!/usr/bin/env python3

import wget
import os
from shutil import copyfile

if os.path.exists("prepy.txt"):
  os.remove("prepy.txt")

url = 'sample_url/file.txt'
wget.download(url, 'path1/prepy.txt')

bad_strings = ['str1', 'str2', 'str3']

with open('prepy.txt') as oldfile, open('postpy.txt', 'w') as newfile:
    for line in oldfile:
        if not any(bad_string in line for bad_string in bad_strings):
            newfile.write(line)
copyfile('/path1/postpy.txt', '/path2/postpy.txt')

cron 作业是通过输入

完成的
sudo crontab -e

并在文件末尾添加上述 cron 作业并保存 cron。这也是通过常规权限完成的,而不是在 "crontab -e" 前面输入 "sudo",而是输入以下

crontab -e

应该意味着sudo不习惯运行文件。

我认为当代码在 cron 中运行时工作目录不是 /home/user/Desktop/。在代码开头添加一行: os.chdir("/home/user/Desktop/")