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

2024-10-01 15:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图诊断一些骗局,我的python代码(Lightning.py)在终端窗口中以“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

我目前正在ubuntu18.04.3中测试这个

唯一发生的事情是,每当cron运行时,它都会创建一个文件“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不用于运行文件


Tags: inpyimporttxturlforosline

热门问题