2

On my raspbian I defined the locale using raspi-config. When I ssh back into it, I can check this is still correct (same result for pi and for root users):

$ locale
LANG=fr_FR.UTF-8
LANGUAGE=fr_FR.UTF-8
LC_CTYPE="fr_FR.UTF-8"
LC_NUMERIC=fr_FR.UTF-8
LC_TIME=fr_FR.UTF-8
LC_COLLATE="fr_FR.UTF-8"
LC_MONETARY=fr_FR.UTF-8
LC_MESSAGES="fr_FR.UTF-8"
LC_PAPER=fr_FR.UTF-8
LC_NAME=fr_FR.UTF-8
LC_ADDRESS=fr_FR.UTF-8
LC_TELEPHONE=fr_FR.UTF-8
LC_MEASUREMENT=fr_FR.UTF-8
LC_IDENTIFICATION=fr_FR.UTF-8
LC_ALL=

Now, I want to start several (python) scripts at startup. I defined and installed this start script:

#!/usr/bin/env sh

### BEGIN INIT INFO

# Provides:          pilberry
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start pilberry

### END INIT INFO

case "$1" in

    start)
        echo "Starting pilberry"
        /home/pi/pilberry/bin/start
        ;;

    stop)
        echo "Stopping pilberry"
        /home/pi/pilberry/bin/stop
        ;;

    reload|restart)
        "$0" stop
        "$0" start
        ;;

    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1

esac

exit 0

I noticed that the python scripts that are started this way raise ascii encoding/decoding exceptions although they work perfectly if I run them from a terminal (also using the very same start script).

Using bash instead of sh doesn't change anything.

I have added this into the shell script:

locale > /home/pi/pilberry/log/locale_at_startup

And here's the result:

$ cat pilberry/log/locale_at_startup                                                                                  
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

I have tried to regenerate the locale (using locale-gen) at the beginning of the startup script (what clearly slows the starting phase), but I get the same result.

I don't really want to try a trick in the python scripts because the problem obviously does not come from python.

So, how is it possible to have the locale set correctly before running scripts at startup?

1

Those are environment variables. I mention this because env variables do not have a universal value; they're inherited from one process to another (so two simultaneous processes may have a different value for $FOO depending on who started them). The locale ones are usually set by PAM when a login is authenticated -- and I mention that because it's a confusing and subtle distinction from variables that get set by login shell configuration files (e.g. /etc/profile).

Anyway, an init service1 never logs in, and therefore anything in either of those categories (PAM or login shell configuration) is not sourced. That's why you'll find some init scripts do something like this:

if [ -f /etc/default/locale ]; then
    source /etc/default/locale
fi

That's the file defining those variables -- it probably matches your output from locale.

So you can do exactly that, but because /etc/default/locale does not actually export anything2, it would only apply to the init script itself and not anything it starts (such as your python program). You could individually export the ones you want as you want them, or you could source that file, then just export pertinent ones without having to set them:

export LANG LC_ALL LC_CTYPE

Or whichever, right after the source line in that if block.


1. Init, being the first process run (the only one ever started by the kernel), has a limited set of hard coded environment variables (such as a basic $PATH) and that's all it passes on to the scripts it starts.

2. Another confusing distinction -- this means they're not really environment variables, they're just variables belonging to a POSIX shell instance. Such a shell will put them into the environment with export.

  • Well, sourcing from /etc/default/locale didn't solve the problem (it did contain several of the variables, but not all; I added some in vain), but exporting some of these locale environment variables did! – zezollo Jun 17 '15 at 20:42
  • Ack! Eek! -- probably because it doesn't export them, so sourcing it would only apply to the script that does the sourcing and not anything it spawns (namely python here). I've added a few paragraphs correcting and explaining this. – goldilocks Jun 17 '15 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.