Skip to content

Python: Script that Execute the First Tuesday of the Month

April 2, 2015

Recently I have been asked to schedule a cronjob to execute an ETL job the first Tuesday of every month.

My first instinct was to add the following line to the crontab:

0 4 1-7 * 2 /dwprod/scripts/jPostDschCallPCM.sh

But, this did not schedule as I was expecting; this executed every Tuesday and every day of the month before the 8th day of the month.

I took this opportunity to use Python to correct the scheduling problem. I was really surprise about how easy I was able to solve this with Python.

Here is my first ever Python script (jPostDschCallPCM.py):

#!/usr/bin/python

import datetime, subprocess

daynum = datetime.datetime.today().day
command_line = [“/bin/ksh”, “-c”, “/dwprod/scripts/jPostDschCallPCM.sh”]

if daynum < 8:
subprocess.call(command_line)

And here is how my new crontab entry looks like:

0 4 * * 2 /dwprod/scripts/jPostDschCallPCM.py

I had scheduled the Python script to run every Tuesday, but inside the Python script I filter for the original script to be executed only if it is the first Tuesday of the month. Even if I could rewrite the original script in Python, I decided to leave it as is was to show how to call another script from a Python Script.

I tested this on Solaris 10 on Sparc with Python 2.4.6

Advertisements

From → Python

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: