Python, Keyring, and Cron

I plug in a python script before running cron (on Ubuntu 12.04) - simple enough. Except for authentication.

The cron script accesses several services and must provide credentials. Saving these credentials using keyring is as easy as it can be, except that when the cron job starts, the credentials cannot be restored. The script crashes every time.

As far as I can tell, this is due to the cron environment running. I tracked a set of messages that suggest that the key has a DBUS_SESSION_BUS_ADDRESS export script. Everything is fine and good - I can get this address and export it, and also fix it from Python quite easily - but it just generates a new error: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11 . Setting DISPLAY=:0 no effect.

So. Has anyone figured out how to unlock gnome-keyring from Python running cron on Ubuntu 12.04?

+7
python cron ubuntu
source share
2 answers

I'm sorry that I have no answer, but I think I know what is happening based on the problem I'm dealing with. I am trying to get a web application and a cron script to use some kind of code that puts the oauth token for the Google API into the firewall using python-keyring.

No matter what I do, something about the environment in which the web application runs and the cron job requires manual intervention to unlock the keychain. This is completely impossible when your code is running in a non-interactive session. The problem persists when you try to use some of the tricks suggested in my research, for example, to provide the process owner with a password for entering the system that matches the password with the keys and set the password for the keys on an empty line.

I almost guarantee that your error stems from Gnome-Keyring while trying to launch an interactive (graphical) tooltip and bomb, because you cannot do it from cron.

+2
source share

Install Keychain:

 sudo apt-get install keychain 

Put it in your $ HOME / .bash_profile file:

 if [ -z "$SSH_AUTH_SOCK" ] ; then eval `ssh-agent -s` fi eval `keychain --eval id_rsa` 

It will ask for your password the first time you log in and save your credentials until the next reboot.

Insert it at the beginning of your cron script:

 source $HOME/.keychain/${HOSTNAME}-sh 

If you use another language, for example python, call it from a shell script.

This works for me, I hope this helps too.

0
source share

All Articles