user1698693
user1698693

Reputation: 1

Python program crashing

So I've designed a program that runs on a computer, looks for particular aspects of files that have been plaguing us, and deletes the files if a flag is passed. Unfortunately the program seems to be almost-randomly shutting down/crashing. I say almost-randomly, because the program always exits after it deletes a file, though it will commonly stay up after a success.

I've run a parallel Python program that counts upwards in the same intervals, but does nothing else. This program does not crash/exit, and stays open.

Is there perhaps a R/W access issue? I am running the program as administrator, so I'm not sure why that would be the case.

Here's the code:

import glob
import os
import time
import stat

#logging
import logging
logging.basicConfig(filename='disabledBots.log')
import datetime


runTimes = 0
currentPhp = 0
output = 0
output2 = 0
while runTimes >= 0:
    #Cycles through .php files
    openedProg = glob.glob('*.php')
    openedProg = openedProg[currentPhp:currentPhp+1]
    progInput = ''.join(openedProg)
    if progInput != '':
        theBot = open(progInput,'r')

        #Singles out "$output" on this particular line and closes the process
        readLines = theBot.readlines()
        wholeLine = (readLines[-4])
        output = wholeLine[4:11]

        #Singles out "set_time_limit(0)"
        wholeLine2 = (readLines[0])
        output2 = wholeLine2[6:23]

        theBot.close()
    if progInput == '':
        currentPhp = -1

    #Kills the program if it matches the code
    currentTime = datetime.datetime.now()
    if output == '$output':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0

    if output2 == 'set_time_limit(0)':
        os.chmod(progInput, stat.S_IWRITE)
        os.remove(progInput)
        logging.warning(str(currentTime) +' ' + progInput + ' has been deleted. Please search for a faux httpd.exe process and kill it.')
        currentPhp = 0
    else:
        currentPhp = currentPhp + 1
        time.sleep(30)

    #Prints the number of cycles    
    runTimes = runTimes + 1
    logging.warning((str(currentTime) + ' botKiller2.0 has scanned '+ str(runTimes) + ' times.'))
    print('botKiller3.0 has scanned ' + str(runTimes) + ' times.')

Upvotes: 0

Views: 372

Answers (2)

Jon Clements
Jon Clements

Reputation: 142136

Firstly, it'll be hell of a lot easier to work out what's going on if you base your code around something like this...

for fname in glob.glob('*.php'):
    with open(fname) as fin:
        lines = fin.readlines()
    if '$output' in lines[-4] or 'set_time_limit(0)' in lines[0]:
        try:
            os.remove(fname)
        except IOError as e:
            print "Couldn't remove:", fname

And err, that's not actually a secondly at the moment, your existing code is just too tricky to follow fullstop, let alone all the bits that could cause a strange error that we don't know yet!

Upvotes: 1

corn3lius
corn3lius

Reputation: 4985

if os.path.exists(progInput): 
    os.chmod(progInput, stat.S_IWRITE)
    os.remove(progInput)

ALSO:

You never reset the output or output2 variables in the loop?

is this on purpose?

Upvotes: 0

Related Questions