HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Displaying a scheduled job under a scheduled time

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
timedisplayingunderjobscheduled

Problem

I use at to schedule jobs and atq to display the scheduled jobs. But it mildly irritates me in that I have to look up each job individually. I wrote a Python script to display the scheduled job under the scheduled time.

(In my world all the commands will begin 'task')

So instead of

stafflinux$atq
8 Mon Aug 18 09:00:00 2014 a joseph
10 Tue Aug 19 09:00:00 2014 a joseph
15 Thu Aug 21 09:00:00 2014 a joseph
12 Fri Aug 22 09:00:00 2014 a joseph
9 Thu Aug 21 09:00:00 2014 a joseph
14 Sat Aug 30 09:00:00 2014 a joseph
7 Sun Aug 17 09:00:00 2014 a joseph
6 Mon Aug 18 09:00:00 2014 a joseph
11 Sat Aug 30 09:00:00 2014 a joseph
stafflinux$


My script produces

stafflinux$./atscript.py
8 Mon Aug 18 09:00:00 2014 a joseph
task "buy a four-way plug adapter"
task "see guy about meeting"
-----------------------------------------
10 Tue Aug 19 09:00:00 2014 a joseph
task "bring back personal effects from office"
-----------------------------------------
15 Thu Aug 21 09:00:00 2014 a joseph
task "book tickets for next week"
-----------------------------------------


I'm looking for any feedback - particularly in terms of 'pythonic' style and any and all tricks I may have missed:

#!/usr/bin/python
import os
os.system("atq > attemp.txt")
file = open("attemp.txt")
line = file.readline()
while line:
number =line[:2]
print line.strip()
os.system("at -c "+ number+ "| grep task")
line=file.readline()
print '-----------------------------------------'
print line
os.system("rm attemp.txt")

Solution

Your script creates a temporary file unnecessarily, uses os.system which is not recommended, and other messy things. Consider this alternative:

#!/usr/bin/env python

import subprocess
import re

re_job_number = re.compile(r'\d+')

for atq_line in subprocess.Popen(['atq'], stdout=subprocess.PIPE).stdout:
    job_number = re_job_number.search(atq_line).group()
    print atq_line.strip()
    for at_line in subprocess.Popen(['at', '-c', job_number], stdout=subprocess.PIPE).stdout:
        if at_line.startswith('task '):
            print at_line.strip()
    print '-' * 40


Advantages:

  • No need for a temporary file to save the atq output, you can process it directly



  • For running shell commands, subprocess is the recommended way, os.system is discouraged



  • No need for a grep process, you can handle the output filtering of at -c in Python itself



Also, writing the first line this way makes the script more portable:

#!/usr/bin/env python


If you write as #!/usr/bin/python then you are effectively hard-coding the Python path, and it might not be the same in all systems.

Finally, I recommend writing print(something) instead of print something, to keep your script ready for migrating to Python 3 someday.

Code Snippets

#!/usr/bin/env python

import subprocess
import re

re_job_number = re.compile(r'\d+')

for atq_line in subprocess.Popen(['atq'], stdout=subprocess.PIPE).stdout:
    job_number = re_job_number.search(atq_line).group()
    print atq_line.strip()
    for at_line in subprocess.Popen(['at', '-c', job_number], stdout=subprocess.PIPE).stdout:
        if at_line.startswith('task '):
            print at_line.strip()
    print '-' * 40
#!/usr/bin/env python

Context

StackExchange Code Review Q#60162, answer score: 3

Revisions (0)

No revisions yet.