xxmbabanexx
xxmbabanexx

Reputation: 8706

How to have python open an application and use it?

I have a Minecraft Bukkit Server which I run on my mac. I also travel often, making server restarts a pain. Is it at all possible for me to have a Python 2.7.3 script which can close the server using the stop command. I would then like the script to wait 30 seconds and then reopen the server. I would like the script to also be on a timer which restarts the server every 6 hours.

My main question is, are there any modules, libraries, or ways for me to accomplish this and where can I find more information if necessary?

print "Hello - This is the Bukkit Restart Program!"
"\n"
print "It will restart the minecraft server every 6 hours"

---- Pseudo Code ----

def RESTART():
    OUTCOME = None
    Every 6 hours:
        if program can open start_server.command:
            print "stop" in start_server.command
            wait 30 seconds
            open start_server.command
            OUTCOME = 1
        else:
             print "Error encountered!!"
             OUTCOME = 0
    return OUTCOME


def check():
    log = open("log.txt", "a+")
    if OUTCOME = 1:
        log.write("\n <insert time> SUCCESS!")
    else:
        log.write("\n <insert time> FAILURE! PLEASE CHECK CODE!")
    log.close()
RESTART()
check()

Sorry if my question is too naive, but I am an amateur programmer!

Thanks!

Upvotes: 1

Views: 131

Answers (1)

Gordon Bailey
Gordon Bailey

Reputation: 3911

A good tool to use for this job would be cron. It comes installed on Mac, and is specifically designed for running tasks at specific intervals.

I would make a python script that runs your command (look into the subprocess module) and does whatever error checking you want, and then set it as a cron job.

The basic procedure to set up a cron job is something like this:

# (in a terminal)
# This opens up your crontab file, which lists all your scheduled jobs
crontab -e

Then add a couple lines like:

0 0 * * *  python /path/to/your/script.py
0 12 * * * python /path/to/your/script.py

That will run your script once at midnight and once at noon each day.

Upvotes: 1

Related Questions