»
S
I
D
E
B
A
R
«
A simple bash script to schedule tasks
Feb 9th, 2009 by sayanriju

For some bizzarre reason, I have never been able to run cron successfully on my system. Mostly, I need functionalities like that offered by cron when I schedule my downloads to stop at the end of ‘happy hours’ of BSNL Broadband (2 am to 8 am).
Instead of trying to google up and solve my problem, I just wrote a simple bash script which will address my needs! The job the script does is quite elementary: it takes as argument a time and a command, and runs the command at that scheduled time.

Here’s the script:

#!/usr/bin/env bash

if [ "$#" -ne 2 -o `echo "$1" | awk -F ":" '{print NF}'` -ne 3 -o "$1" == "-h" -o "$1" == "--help" ];then
	## Print uasge instructions
	echo -e "Usage:	 mycron  \n	(Both arguments are mandatory)\nRuns specified command at specified time\n"
	echo -e "Note: You must specify the time for execution EXACTLY as HH:MM:SS\n"
	echo -e " E.g.: 09:00:00 instead of 9:00:00 or 09:00 "
	echo -e "\nAlso, 24 Hour clock format is expected,\nso use 21:00:00 instead of 09:00:00 if you mean PM\n\n"
	echo -e "mycron -h, --help : Display this help message and exit\n"
	exit
fi

while true;do
    ctime=`date | tr -s ' ' | cut -d' ' -f4 `
    if [ "$ctime" == "$1" ];then
	"$2" &
	echo -e "Process $2 with PID $! has been run at $ctime\nAborting script $0"
	exit
    fi
done

Of course, this script can be made to do things a lot better, but right now, it does exactly what I need!

Plans for an Ebook Collection Manager in PHP for DOEACC Project
Jan 29th, 2009 by sayanriju

I submitted the guide selection form for doing my DOEACC A Level projects yesterday. I had to choose a ‘Software platform’ to code on. My primary choice was of course Python, but unfortunately there’s no guide available at the Institute with any expertise in Python. Hence, I had to choose PHP/MySql as my language! :-(
The trouble is, I don’t know PHP at all! :P I’ve begun to learn it from some ebooks and w3schhols, and so far, my take on the language is pretty good.
The syntax is not as lucid and natural as Python (obviously!), but I’m really enjoying the way it can be used to manipulate HTML stuff! Since I’ve never done any web programming at all, this entire concept is like a new toy for me. Currently, I’m playing with connecting to a mysql database server (I’m fairly comfortable with SQL stuf) and viewing the result of my queries on the browser. The only time I need to stop is to check on with HTML syntax, because I am not at all well versed with HTML at all. Perhaps a bit of Javascript knowledge would also come in handy.

The idea of the project I have in mind is to develop some kind of a Ebook Library Manager. For long, I myself have felt the need of such an application to manage my reasonably large collection (nearly 2 gigs) of ebooks I have on my hard disk. None of the existing collection managers (like GCstar, Alexandria,etc) catered to my needs, so I needed to write an application of my own. Originally, my plan was to do it in Python with an Sqlite3 database backend and WxPython for the GUI. Now, Php would replace Python, MySql would replace Sqlite, and of course, there would be no need for a GUI toolkit as the frontend will be handled on the web browser itself by using HTML/JS. This is a rather good thing because I won’t have to worry about the WxPython codes, which is quite difficult & time consuming to perfect(esp. the layouts & alignments) in absence of a proper RAD tool. Also, the program will now be web-based & hence platform independent with no need for a specific GUI toolkit to be installed. On the downside, using PHP and MySql makes running a webserver like Apache mandatory, which sounds a bit overkill & out-of-context since the purpose of the program is to manage local ebooks. And I have to learn PHP, HTML and Javascript fairly well before starting the project.

I hope that this idea qualifies as a valid A Level Project, esp. with the aprox. 350 man hours involvement requirement posed my DOEACC. Formal allocation of guide and commencement of projects will begin from 3rd week of February. I intend to have a working draft of the project by then to show. Till then, onwards to w3schhols and the PHP Manual!

A small Python script for merging PDFs
Jan 27th, 2009 by sayanriju

I am a frequent downloader of ebooks in pdf format (yeh…I know its piracy and all! :P).
Very often the uploaders put up the ebook in parts, probably because of the large size of unsplitted pdfs. Here’s a simple script I wrote in Python to merge several pdf-s neatly in order.

#!/usr/bin/env python

from pyPdf import PdfFileWriter, PdfFileReader
import sys

opfile=sys.argv[-1]
output = PdfFileWriter()

for ipfile in sys.argv[1:-1]:
	input=PdfFileReader(file(ipfile, "rb"))
	for pnum in range(0,input.getNumPages()):
		output.addPage(input.getPage(pnum))
outputStream = file(opfile, "wb")
output.write(outputStream)
outputStream.close()

Its really easy to use it. Save it as pypdfmerge.py, and run it as:
python pypdfmerge.py  <input_file1> <input_file2> <input_file3> .... <output_file>
The last argument is always taken as the output file.
Note that it will throw some Deprecation warnings, but they are generally harmless, so ignore them.

»  Substance: WordPress   »  Style: Ahren Ahimsa
© Sayan Chakrabarti (http://sayanriju.co.cc)