Saturday, February 9, 2013

getch in python

Few days ago I started learning Python for one of the classes I'm taking. In the very first Python program I wrote, after an answer was displayed I wanted to wait for a key input from user before the next line was executed. If I was programming using C or C++ this could have been done easily using getch() function. Therefore I searched for something equivalent in Python. After reading couple of threads I realized that Python does not have something similar to getch. May be I'm wrong, there is something similar to getch and I couldn't find it. However I was able to use a simple way to produce the same effect I was looking for using the Python's input command.

input("")

Since input command prompts a massage to the user and assigns the user input to a variable, using input("") without any argument and without any variable assignment, I was able to pause the program execution until user hit 'enter'. It's not quite the same as getch but it's close. If you are looking for a getch equivalent in Python this might be useful for you.

Scheduling a task in Linux using crontab

Recently, when I was working on my research I had to check whether a particular file was updated the way I wanted. One way of doing this was to sit by my computer and continuously check the file whenever it updated. This file was created from a continuous process that can run for days until was killed. Obviously I did not want to sit by my computer for days looking at that file. Fortunately there is another way to do this in Linux systems. Linux has a utility called 'cron' to do this task.
In my case, I created a small shell script to copy the file in question to a different location with a different file name. Every time I run that script, it creates a copy of the file I want to a different location with a new file name. Now I had to set up a way to run the script every few minutes automatically, so I can come back later and check the new set of files.
If you are looking for a way to do a similar task, you also can use cron utility in Linux.

To do this, first you have to open up the cron utility. Go to the terminal and type,

crontab -e

Now, to schedule a task you have to follow the format as follows,

MIN HOUR DOM MON DOW CMD

MIN    - Minutes, can have values from 0 - 59
HOUR - Hours, can have values from 0 - 24
DOM   - Day of the month, can have values from 1 - 31
MON   - Month, can have values from 1 - 12, 1 being January
DOW   - Day of the week, can have values from 0 - 6, 0 being Sunday
CMD   - The command you want to execute

So Let's say I want to execute a script in my 'Documents' directory every 30th minute, every hour, everyday. In that case I can use the following line in my cron

30 * * * * /home/usr/documents/script_name.s

asterisk(*)  is used to represent all the values in a particular field. Which means my "script_name.s" file will be executed every 30th minute of every hour, every day, every month.

Let's look at another example. Let's say I want to run my script every weekday at 6.00 p.m,

0 18 * * 1-5 /home/usr/documents/script_name.s

what if want to run my script every 5 minutes

*/5 * * * * /home/usr/documents/script_name.s

You are not limited to run scripts with cron. You can execute any command you want. For example, when I first found out about cron, I used the echo command to write a line into a file every minute just to check if I was doing it correctly.

*/1 * * * * echo "testing cron">>/home/usr/documents/cron_test.txt

These examples will give you an idea on different ways you can use cron utility to schedule your tasks. As you can see, you have lot of flexibility in scheduling tasks using cron. Once you type your cron command, just save the cron file and you're done.

You can schedule multiple tasks using cron and if you want to comment out any line from the cron file, you can use # symbol to do so.