Popular Posts

Search This Blog

Tuesday, December 31, 2013

Writing CRON jobs on a Mac

Mac OSX is essentially a UNIX distribution, so we have all the cool under-the-bonnet tricks and tools of any operating system with a UNIX backbone. One of the utilities I’ve always found useful in a UNIX OS is the CRON tab:

“Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.”

To set up a new Cron job, open your Terminal and type:
sudo crontab -e

You’ll be prompted for your password, and upon correct submission, will open a blank VI window.

The basic format of a Cron is:
0 0 * * * sh /directory/script.sh
    1.    The first 0 denotes minutes past the hour (0 is on the hour)
    2.    The second 0 denotes the hour of the day (0 is 12:00am)
    3.    The first * denotes day of the week (* means every day, 1 is Monday etc)
    4.    The second * is the week of the month (* means every week, 1 is the first week of the month)
    5.    The final * is the month of the year (* means every month, 1 is the first month of the year)

We specify the type of script we wish to execute; in the example above, we’re executing a basic shell script, and the final parameter details the absolute location of the script you wish to execute at regular intervals.

I often execute daily PHP scripts on my server, an example of a PHP script (backup.php) running every day at 6pm is:
0 18 * * * php ~/site/cleanup.php

Note the script type is now php, as we’re executing a php script, not a shell script.
Finally, let’s write a script that backs up my ssh keys from ~/.ssh to my Dropbox directory, once a week, on a Monday at 9:30am.
Using Terminal.app, open a new file for editing anywhere on your system (I use vi).
sudo vi ~/src/backup.sh

Copy and paste the script below, write to backup.sh and exit vi.
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/Users/*yourmachine*/logs/$(date +%Y%m%d)_rsync.log /Users/*yourmachine*/.ssh /Users/*yourmachine*/Dropbox

The script uses rsync to backup the directory at “/Users/*yourmachine*/.ssh” to your Dropbox directory at “/Users/*yourmachine*/Dropbox”. It’ll also log the backup in a logs directory in your home directory. Now for the Cron.

30 9 1 * * sh ~/src/backup.sh

Hopefully this will get you started with using Cron on your OSX system.

Sunday, December 29, 2013

Some Generic Functions

#################### Start of the Program

#!/usr/bin/python

import csv
import xlwt
import xlrd
import sys
import urllib2
import json

1) Convert csv file from excel file
excel_file -- Excel file name which you want to convert
sheet_name -- Name of the sheet mention in Excel file which you want to convert
csv_file -- Csv filename for which you are converting from excel file.

This is generic funtion to convert data from excel file to csv file which you can use for wider purpose and no matters about amount of data.

def csv_from_excel(excel_file,sheet_name,csv_file):
    wb = xlrd.open_workbook(excel_file)
    sh = wb.sheet_by_name(sheet_name)
    your_csv_file = open(csv_file, 'wb')
    wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
    for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))
    your_csv_file.close()

2) Code to change excel font in excel file 
clr_index -- This is generic color indexex to change color font

def excel_font(clr_index):
    font0 = xlwt.Font()
    font0.name = 'Times New Roman'
    font0.colour_index = clr_index
    font0.height=240
    font0.bold = True
    style0 = xlwt.XFStyle()
    style0.font = font0
    return style0

3) Code for bold font
def bold_font():
    style = xlwt.easyxf('font: bold 1, height 240')
    return style

4) Code for font height
def font_height():
    style = xlwt.easyxf('font: height 240')
    return style

5) Code to get column parameters from excel file
col_index -- Column Index from which you want to get parameters given in excel file. 

def get_column_params(excel_file,sheet_index,col_index):
    workbook = xlrd.open_workbook(excel_file)
    sheet = workbook.sheet_by_index(sheet_index)
    data = sheet.col_values(col_index,start_rowx=1)
    return data

6) Convert values from csv file to dictionary (data structure in python)
def csv_to_dict_params(csv_file,excel_file,len_of_sheets):
    d_parameters={}
    data=get_column_params(excel_file,len_of_sheets,1)
    column_len=len(data)
    with open(csv_file, 'rb') as f:
        rows = list(csv.reader(f))
        while column_len !=0:
            d_parameters[rows[column_len][1]]=rows[column_len][2]
            column_len=column_len - 1
    return d_parameters

7) Convert values from coming json to dictionary (as mentioned earlier,  (key,value) pair)
request_url -- url which returning json

def json_to_dict(request_url):
    req=urllib2.Request(request_url)
    response=urllib2.urlopen(req)
    full_resp=response.read()
    json_dict={}
    json_list=json.loads(full_resp)
    if type(json_list) is list and len(json_list)>0:
        temp_dict=json_list[0]
        for k,v in temp_dict.items():
            if type(v) is not dict:
                json_dict[k]=v
            else:
                for d_k,d_v in v.items():
                    json_dict[d_k]=d_v
    elif type(json_list) is dict and len(json_list)>0:
        for k,v in json_list.items():
            if type(v) is not list:
                json_dict[k]=v
            elif len(v)>0:
                json_dict[k]=""
                temp_dict=v[0]
                if type(temp_dict) is dict:
                    for k,v in temp_dict.items():
                        json_dict[k]=v
    return json_dict

########################## End of the Program

My Profile

My photo
can be reached at 09916017317