Tinkering with Raspberry (and other things)

Quick Tip: crontab and python

Often running a python script via crontab does not work as expected. The most common problem is with permissions, I am sure everybody is aware of.

Here are some other reasons why calling a python program from crontab may fail:

  1. The last line in the crontab file needs to be a newline
  2. Scripts called from within crontab may not contain a “.” (dot) character. This is weird and almost unknown to most users.
  3. crontab scripts may not have all PATH environments you have while testing your python program


So what to do about it? Here’s an easy solution:

Suppose you have added an import-path to your python installation like I did in this quick-tip. We are going to “wrap” the call to “” in a bash script and name that script “ourprogram_starter”. No dots, crontab is happy. In this script we export the PATH variable so that crontab and the “ourprogram_starter” can find all our custom modules.

export PYTHONPATH="$HOME/some_Directory_Name"
python /full/path/to/

Make sure that this file is executable and you’re good to go. Now crontab can call this script which sets all needed environment variables and then calls the python program.



Quick Tip: Adding a directory to Python’s module search path

Over time I have written some Python modules/classes that I am using in more than one little project. As these are single files mostly, I did not bother about writing “real” modules with “” and all that stuff. I do not want to install these files in Python’s default library directory, either.

So I did what one should never ever do 🙂 I copied the files into the project directories as needed. This quickly gets out of hand if you are doing some updates to one of the classes. In order to get some robust structure into my workplace on the Raspberry Pi I did some research and reading about installing Python modules and came up with the most simple solution I can think of: adding my own directory to Python’s search path.

In the Python documentation everything I did can be found, of course. As I did miss it during several reads, I am posting my way of doing it here. To remind me and to, hopefully, help others.

First, a directory to put all the modules to be reused into needs to be created:

mkdir some_Directory_Name

Now we need to add an environment variable that gets set when you log in. Go to your home directory and edit the file “.profile”:

nano .profile

At the end of the file add the following lines:

if [ -d "$HOME/Workplace/Python/MyModules" ] ; then
    export PYTHONPATH="$HOME/some_Directory_Name"

That’s pretty all there is to do. The next time you log in the variable PYTHONPATH is set and used by Python. The if-statement checks for the existence of the given directory. The environment variable will only be created if the directory exists.

You can check this. Do not log out yet, but start Python and do the following (screenshot for better readability:

Bildschirmfoto 2013-12-04 um 14.57.10

This gives you the directories where Python looks for files when you are using the import-statement. Now to have our changes take effect, we could log out and in again, or we re-read the .profile with:

. ~/.profile

There needs to be a space between the first dot and the tilde character. If you are now starting Python again and do the exact same thing you should see your path somewhere in the output from sys.path:

Bildschirmfoto 2013-12-04 um 14.57.59

I have highlighted my personal search path in the above picture.

That’s all for this quick tip.

Edit Source Code Remotely

Recently I caught myself juggling with 5 open terminal sessions simultaneously. Four windows were showing different source files from a project I am working on and the fifth terminal was the commandline from where I started compilation and ran the programs. This was really annoying because even on my 27″ monitor the terminal windows took all the space and I frequently had to change desktops and so on.

So the question arose: “Can’t this be done in a more efficent way?” Continue reading


Real Time Clock (reloaded)

I could not stop thinking about the RTC and the Raspberry. In my personal setup, the RTC is not always connected to the Raspberry, so it would not make sense to throw away the fake hardware clock and rely on the RTC only. I needed a solution that was most flexible and with the smallest possible overhead.

The solution I came up with is to have a script check for the RTC on startup and create the device only when the RTC is there. This means tinkering around with “autostart” functionality and I am not going to edit rc.local, but do it the more correct and formal way. So if you want to learn how to run scripts on boot/reboot read on! Continue reading

1 Comment

Basics for Beginners (part 3 – SSH)

Welcome to the third part of what in reality is just a compilation of my notes…

My Raspberry is running “headless”. Most of the time. This means that no keyboard, mouse or display is attached to it. The only means of access is via a secured shell, SSH in short. I will not go into detail on how to activate SSH on the Raspberry, this is extensively described in every “First steps” guide I know. So from now on I will suppose that you have successfully logged into your Raspberry via SSH from a remote machine.

Continue reading

1 Comment

Basics for Beginners (part 2 – backup/restore)

The Basics for Beginners (part 1) got rather lengthy so I decided to split it into two three parts. In this second part it’s all about backup and accessing the Raspberry Pi via SSH.

At first I did not care about backing up the system. If something went wrong I just dumped a new and clean image onto my SD-card and started all over. With more and more bits and pieces coming together and (some of them) working out, I soon realized that it would be a good idea to regularly backup the things I had accomplished so far.
Remote access is another important thing. I am sure most of us have the Raspberry Pi running as a “headless” system, connecting to it via SSH from a shell. However, there is one drawback with SSH. It generates a key per IP address. Now I wanted to use just one IP for my Raspberry, no matter which SD-card was inserted. This usually goes not very well with SSH, as different systems (per SD-card) will result in different SSH-keys. Continue reading

Basics for Beginners (part 1 – go easy)

There are numerous guides for beginners, so I won’t repeat all the steps. As a Mac user I will concentrate on this system.
Of course Raspberry’s own Wiki is a great source of information. They do have a tutorial how to set up an SD-card on various systems. Once you have set up the card put it in the Raspberry, connect a display, network and keyboard and fire it up. I will not get into detail here. The two things where most users are insecure are “How to switch it off” and “How to backup my system”. So I will discuss these here. Continue reading