1

Noob question here. I'm scheduling a cron job for a Python script for every 2 hours, but I want the script to stop running after 48 hours, which is not a feature of cron. To work around this, I'm recording the number of executions at the end of the script in a text file using a tally mark x and opening the text file at the beginning of the script to only run if the count is less than n.

However, my script seems to always run regardless of the conditions. Here's an example of what I've tried:

with open("curl-output.txt", "a+") as myfile:
    data = myfile.read()

    finalrun = "xxxxx"

    if data != finalrun:
        [CURL CODE]

        with open("curl-output.txt", "a") as text_file:
            text_file.write("x")
            text_file.close()

I think I'm missing something simple here. Please advise if there is a better way of achieving this. Thanks in advance.

7
  • 1
    Why are you using a tally mark? Can't you just use a number? Have you looked at cPickle to easily store data on disk? Commented Apr 16, 2015 at 18:44
  • Can you be a little more clear ? You mention that you append an x each time the script is run, so correct me if I am wrong here, but in 48 hours shouldn't the number of x's be 24 ? you are testing the data against 5 x's . Commented Apr 16, 2015 at 18:48
  • 1
    Have you looked at curl-output.txt to make sure that it looks like what you want? Commented Apr 16, 2015 at 18:49
  • @Bhargav Yes, it should be 24 xs. I've used 5 as an example. Commented Apr 16, 2015 at 18:49
  • 1
    As an aside, if len(data) < 24 would be a safer test. Commented Apr 16, 2015 at 19:17

5 Answers 5

2

The problem with your original code is that you're opening the file in a+ mode, which seems to set the seek position to the end of the file (try print(data) right after you read the file). If you use r instead, it works. (I'm not sure that's how it's supposed to be. This answer states it should write at the end, but read from the beginning. The documentation isn't terribly clear).

Some suggestions: Instead of comparing against the "xxxxx" string, you could just check the length of the data (if len(data) < 5). Or alternatively, as was suggested, use pickle to store a number, which might look like this:

import pickle
try:
    with open("curl-output.txt", "rb") as myfile:
        num = pickle.load(myfile)
except FileNotFoundError:
    num = 0

if num < 5:
    do_curl_stuff()
    num += 1
    with open("curl-output.txt", "wb") as myfile:
        pickle.dump(num, myfile)

Two more things concerning your original code: You're making the first with block bigger than it needs to be. Once you've read the string into data, you don't need the file object anymore, so you can remove one level of indentation from everything except data = myfile.read().

Also, you don't need to close text_file manually. with will do that for you (that's the point).

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried opening the file with r and gotten an error, hence the a+, but it seems to be working fine now. I removed a level of indentation and the text_file.close(). I'm going with if len(data) < 24: for now which works smoothly. I will most likely end up using a += 1 counter method for more sophistication. I am just trying out different things to learn Python better. Thanks for the suggestions!
1

Sounds more for a job scheduling with at command?

See http://www.ibm.com/developerworks/library/l-job-scheduling/ for different job scheduling mechanisms.

Comments

1

The first bug that is immediately obvious to me is that you are appending to the file even if data == finalrun. So when data == finalrun, you don't run curl but you do append another 'x' to the file. On the next run, data will be not equal to finalrun again so it will continue to execute the curl code.

The solution is of course to nest the code that appends to the file under the if statement.

2 Comments

Sorry, that was an editing error. That part is actually nested in the if statement. Fixed in original post.
Ok well at least that won't confuse anyone else anymore.
1

Well there probably is an end of line jump \n character which makes that your file will contain something like xx\n and not simply xx. Probably this is why your condition does not work :)

EDIT

What happens if through the python command line you type

open('filename.txt', 'r').read() # where filename is the name of your file

you will be able to see whether there is an \n or not

3 Comments

For the \n to be present it has to be added in the script it self right ?
@tgdn There doesn't seem to be any \n when I open the text file. Is it possible that it is still there?
You could do print repr(open('filename.txt', 'r').read()) to see if there are any new lines. But your script doesn't write any and I cant figure out why @tgdn thinks this is the answer.
0

Try using this condition along with if clause instead.

if data.count('x')==24

data string may contain extraneous data line new line characters. Check repr(data) to see if it actually a 24 x's.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.