I have written a Python script that I would like to run in the background on my Raspberry Pi. The script makes use of the Python module Keyring (link), which is used to access a keyring backend to provide password input to the script.

I wrote the script in OS X, for which Keyring makes use of OS X Keychain. But on a headless Raspberry Pi not using X11, something like GNOME Keychain must be installed to act as the backend.

I have read the Pypi instructions (referenced below), but I am unfamiliar with D-Bus and how to make it all work in practice. Would anyone be willing to provide a practical example of how D-bus and the GNOME Keyring may be handled by Raspbian, assuming this:

  • The script should always run in the background as long as the Raspberry is powered on, and it is invoked via crontab.
  • The script should run from a virtualenv environment, which has the Keyring module installed.
  • Handling of the GNOME Keyring/D-Bus session is handled as automatically as possible, and can provide the keyring entries to the script without user input (e.g. loaded automatically when the Raspberry is powered on, or the script is executed).


Using Keyring on headless Linux systems

It is possible to use the SecretService backend on Linux systems without X11 server available (only D-Bus is required). To do that, you need the following:

Install the GNOME Keyring daemon. (Note: I have done this via apt-get).

Start a D-Bus session, e.g. run dbus-run-session -- sh and run the following commands inside that shell.

Run gnome-keyring-daemon with --unlock option. The description of that option says:

Read a password from stdin, and use it to unlock the login keyring or create it if the login keyring does not exist.

When that command is started, enter your password into stdin and press Ctrl+D (end of data). After that the daemon will fork into background (use --foreground option to prevent that).

Now you can use the SecretService backend of Keyring. Remember to run your application in the same D-Bus session as the daemon.

1 Answer 1


You still have to follow the instructions and get the required context up and running. This is normally done when logging into the graphical shell, but since you use a headless system, you have to create the environment.

That means either you login (using ssh) and run the programs from the commandline as specified in "Using Keyring on headless Linux systems) and then start your own program, or you make a script which does all those actions.

If your program is to be started from crontab, the second option (making a script that does all the required things and then start your Python program) is the way to go.

Please note that your environment when running a script from cron is different, including a very restricted PATH where commands are searched.

Running from a virtualenv is automatic if your program is installed as a package entrypoint (i.e. you did /path/to/your/virtualenv/bin/python setup.py install ). If you didn't make your program into a package, it suffices to start it as:

/path/to/your/virtualenv/bin/python /path/to/your/main/python/prog.py

to actually use all the packages installed in your virtualenv.

You should be able to do the steps specified from your python program, but this deviates from the description (that assumes you have a shell). So unless you are experienced in those matters, I would stick with making a shell script that does the steps, and then calls your python program.

  • 1
    I don't have dbus-run-session installed myself, so I am not sure, but this should work similar to using ssh-agent so what you do is start a shell (sh) in the context of a dbus session, all the further commands should run in this shell. In your case you can also start the shell script that does everything needed.
    – Anthon
    Mar 30, 2016 at 10:25
  • Thanks Anthon. There seems to be a pending bug in python-keyring that makes it unusable with gnome-keyring for the time being. I will have to come back to this issue once it's resolved (in the meantime I might have to downgrade to a previous version of keyring).
    – P A N
    Mar 30, 2016 at 10:32
  • 1
    @Winterflags There seems to be an python-keyrings-alt package. And you can of course also install directly from PyPI using pip, that version is newer.
    – Anthon
    Mar 30, 2016 at 10:39
  • I will try to locate a previous version of keyring. The python-keyrings-alt repo doesn't actually exist yet: bugs.archlinux.org/task/47907
    – P A N
    Mar 30, 2016 at 10:40
  • I installed a depreciated version of keyring, but I'm still having problems getting gnome-keyring-daemon to work in dbus-run-session. When I follow the instructions and start the daemon, and then open a new window for the shell and try to run keyring, it still doesn't find the backend. It's made more difficult because the daemon doesn't give any text feedback to the shell. Hope someone knows how to work it!
    – P A N
    Mar 30, 2016 at 11:17

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.