run startup script which requires a xserver session

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)

Answer

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]
Name=Name
Exec=/bin/bash -c "sleep 15 && python /path/to/script.py"
Type=Application

Attribution
Source : Link , Question Author : hirsch , Answer Author : Community

Leave a Comment