简体   繁体   中英

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

I am trying to diagnose some shenanigans, where my python code (Lightning.py) executes in a terminal window with "sudo ./Lightning.py" but not withing my cron job.

I am rather new to linux and would welcome any assistance!

Here is the code for 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')

And here is the associated cron job. It was set at every minute so I could test it.

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

I am currently testing this in Ubuntu 18.04.3.

The only thing that does happen is that whenever the cron runs, it creates a file "prepy(1).txt", but doesn't sort through and delete lines and save it as the correct output.

As an added bonus, here is a sample of the downloaded file from the script:

;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

Additional Info:

Here is a simplified version of the .py file

#!/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')

The cron job was made by typing in

sudo crontab -e

and adding the above cron job at the end of the file and saving cron. This was also done with regular permissions, where rather than typing "sudo" in front of "crontab -e" the following was entered

crontab -e

This should mean that sudo isn't used to run the file.

I think the working directory is not /home/user/Desktop/ when the code runs in cron. Add a line at the beginning of the code: os.chdir("/home/user/Desktop/")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM