Question

I take notes in class on my computer and share these notes via a public folder on dropbox. When I take notes in class, I create a lot of unnecessary files (I take notes in LaTeX) before I generate a PDF. I don't want to clutter my dropbox space with the unnecessary files, and would rather post only the PDFs to dropbox.

In order to facilitate all of this, I set up a cronjob that runs a python script (below) after every class (weekly). Sometimes, I stay back for a few minutes while I fix something in my notes before I export a PDF, so the python script has a bunch of sleeps in it, waiting for the PDF to be generated. I accidentally manually ran that script today, and need help stopping it.

import os
import subprocess

from sys import exit as crash
from datetime import date as dt
from time import sleep

def getToday():
 answer = dt.strftime(dt.today(), "%b") + str(int(dt.strftime(dt.today(), "%d")))
 return answer

def zipNotes(date):
 today = getToday()
 while 1:
  if today not in os.listdir('.'):
   with open("FuzzyLog", 'a') as logfile:
    logfile.write("Sleeping\n")
   sleep(60*5) # sleep 5 minutes
   continue
  if "Notes.pdf" not in os.listdir(today):
   with open("FuzzyLog", 'a') as logfile:
    logfile.write("pdf not exported. Sleeping\n")
   sleep(60*5) # sleep 5 minutes
   continue

  subprocess.call("""zip Notes.zip */Notes.pdf""", shell=True)
  crash(0)

zipNotes(getToday())

Since the script doesn't find any files made today (I could easily just create a dummy file, but that's not a "proper" solution), it loops through the sleep condition infinitely. Since the looping conditions are quite simple, I can't count on the process to be active for very long to "catch it in the act" to get its PID to kill it.

ps aux | grep python doesn't show me which PID is running the python script I want to kill, nor does ps -ax | grep python or ps -e | grep python.

Does anyone have any idea how I can track a python script while it's sleeping?
I'm on Mac OSX 10.7.5 (Lion), if that matters

Was it helpful?

Solution

The traditional way Unix daemons handle this problem is with "pid files".

When your program starts up (or when your launcher starts your program up), it creates a file in a well-known location (/var/run/<PROGRAM NAME>.pid for system daemons; per-user daemons don't have a universal equivalent), writes the PID into that file (as in the ASCII string 12345\n for PID 12345), and deletes it when it exits. So, you can just kill $(cat /var/run/myprogram.pid).

However, it doesn't seem like you need this here. You could easily design the program to be cleanly shutdown, instead of designing it to be killable.

Or, even easier, remove the sleep; instead of having cron run your script it every hour and having the script sleep for a minute at a time until the PDF file is created, just have LaunchServices run the script when the PDF is created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top