I have a python3-script which uses the Gtk-module (imported from gi.repository, for recent-manager functionality), requiring a link to the current xserver session (lightdm service in Ubuntu).

Running it manually via console (in a xserver session) works, but i want it to autorun.

An init.d script won't work (setting the DISPLAY variable to 0.0). Using xvfb-run made the init.d script run my script, but in a virtual xserver environment as expected (not mine). Is there no equivalent, which connects a init.d script to the current xserver-session?

So far i've tried an .xinitrc file (in ~/, yes it is executable), as well as creating .xprofile, .xsession, .xsessionrc which all link to the first one via ln -s. I've tried to add gnome-terminal & for testing but the terminal didn't show up either, be it after either restarting lightdm or even the entire PC and logging in again.

I've also tried Run configuration script at X session resume, but that gives the error Can't open /usr/share/acpi-support/power-funcs.

Using upstart, as suggested by Run a startup script with lightdm wouldn't provide the xserver display variable (or handle?) either, just run it after lightdm started, correct?

Maybe put it somewhere in xorg.conf? (How can I make xrandr customization permanent?) Beats me..

(My script makes quicklists of recent files and adds them to the unity launcher's rightclick-menues, similar to windows: https://github.com/thirschbuechler/ubuntu-recentquicklists)

  • Since your script is for making changes to the Unity launcher, presumably you want it to run when you login, right? See askubuntu.com/a/518440/158442 or askubuntu.com/a/500147/158442 (Also, which version of Ubuntu?) – muru Feb 11 '16 at 16:39
  • Hi hirsch, did you notice you've got an answer? – Jacob Vlijm Feb 12 '16 at 14:25
  • @JacobVlijm: yes i noticed your answer, thank you! I have to try it out myself before upvoting tough, and will do so in the course of this week i hope. muru: thx too, i'll look into it. I have 14.04, but i want it to run on any ubuntu version, if possible – hirsch Feb 12 '16 at 16:57
  • Please see this: meta.askubuntu.com/questions/15051/… Testing the answer takes 20 seconds + a restart. – Jacob Vlijm Feb 14 '16 at 12:16
  • apart from the fact that i discovered i have a different issue, you could have tested it yourself too, since i've provided the script, and seen that it doesn't work. I am sorry, because i may have to rephrase my question @stackexchange since the problem seems to be linked to the dbus interface in pygtk. My apologies – hirsch Feb 15 '16 at 14:44

As in many cases, timing is the main issue here.

  1. Your script needs to run on user level after log in, since it edits launcher items, which is on user level.
  2. Even then, looking at what the script should do (reading your explanation), the script needs to wait a bit for the desktop to fully load; the Unity Launcher is not the first thing to be "ready" after log in. If the script runs too early it will either break or miss target.

In short

You need to run the script appr. 10-15 seconds after log in, which can be done by adding either:

/bin/bash -c "sleep 15 && python /path/to/script.py"

(if the script is not executable), or

/bin/bash -c "sleep 15 && /path/to/script.py"

(if the script is executable) to the user's Startup Applications: Dash > Startup Applications > Add.

Run it for all users

If you need the script to run for all users (after log in), You can add it to Startup Applications for all users at once.

To do that, copy the code below into an empty file, edit the lines, starting with Name= and set the correct path to the script in the line:

Exec=/bin/bash -c "sleep 15 && python /path/to/script.py"

then save the file in /etc/xdg/autostart as launch_script.desktop (or whatever you like, as long as it ends with .desktop)

[Desktop Entry]
Exec=/bin/bash -c "sleep 15 && python /path/to/script.py"

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.