Last active
December 11, 2015 06:19
-
-
Save fstrube/4558821 to your computer and use it in GitHub Desktop.
This simple script will list all of your entries in your crontab file, and allow you to choose which one to run immediately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from subprocess import call, Popen, PIPE | |
import re | |
def main(): | |
""" | |
Prompts the user which cronjob they would like to run, then | |
runs it in the shell. | |
""" | |
# Retrieve the list of cron jobs from the shell | |
p_crontab = Popen(["crontab", "-l"], stdout=PIPE) | |
cronjobs = p_crontab.communicate()[0].split("\n") | |
# Filter blank lines and comments | |
re_comment = re.compile(r"^#") | |
cronjobs = filter(lambda x: x != "" and not re_comment.match(x), cronjobs) | |
print "Which cronjob would you like to run?\n" | |
# Build a numbered menu of cron job commands | |
i = 0 | |
for job in cronjobs: | |
i = i + 1 | |
command = parse_command(job) | |
print " %d) %s" % (i, command) | |
print " q) Quit. Do not run anything.\n" | |
# Process the users input | |
while True: | |
run = raw_input("Enter number: ") | |
# The 'q' option will quit | |
if run == "q": | |
break | |
# If a valid option is specified, run the command and quit | |
if run.isdigit() and int(run) > 0 and int(run) <= len(cronjobs): | |
command = parse_command(cronjobs[int(run) - 1]) | |
print "\nExecuting: %s" % command | |
print "=" * 80 | |
call(command, shell=True) | |
break | |
# Otherwise repeat the loop | |
else: | |
print "Error: Invalid option" | |
def parse_command(job): | |
""" | |
Given a single line in crontab syntax, return the command. | |
""" | |
parts = re.compile(r" +").split(job.strip()) | |
command = ' '.join(parts[5:]) | |
return command | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment