1

I'm trying to create a systemd (user) timer that opens certain tabs every day at 9PM with xdg-open. Here are the files:

/home/me/.config/systemd/user/test.service

[Unit]
Description=Test

[Service]
Type=simple
Environment=DISPLAY=:0
ExecStart=/bin/bash /home/me/test.sh

/home/me/.config/systemd/user/test.timer

[Unit]
Description=Test Timer
RefuseManualStart=no
RefuseManualStop=no

[Timer]
Persistent=true
OnCalendar=*-*-* 21:00:00

[Install]
WantedBy=timers.target

/home/me/test.sh

#!/bin/bash


websites=(
    "http://unix.stackexchange.com/"
    "http://stackoverflow.com/"
)

for i in "${websites[@]}"
do
    # works with /usr/bin/firefox
    /usr/bin/xdg-open "$i"
done

This doesn't open anything.

When I replace xdg-open with firefox in the above file, the tabs get opened. However, when I execute xdg-open "http://unix.stackexchange.com/" in a terminal it opens a tab in Firefox.


systemctl --user start test.service 
systemctl --user status test.service 

prints:

● test.service - Test
   Loaded: loaded (/home/me/.config/systemd/user/test.service; static; vendor preset: enabled)
   Active: inactive (dead)

Jun 29 15:06:59 me-PC systemd[1513]: Started Test.

How can I make this timer work with xdg-open?

  • define "works as expected". You mean, it xdg-open caused a new tab to be opened in Firefox? – Mark Stosberg Jun 29 '16 at 13:57
  • @MarkStosberg yes! I updated my question. – Joschua Jun 29 '16 at 14:00
2

Often differences between running a script manually and running it via systemd are due differences in the environment. Before the xdg-open call, add the command env on it's own line, which dumps the environment.

Now run the test manually and via systemd. Look for other variables besides DISPLAY that could be causing the difference. By continuing to add environment variables into the systemd script, you should be able to find the ones systemd needs to make this work.

Your bash code in your test looks fine, but here's a revision that's a bit more idiomatic if you are interested. bash naturally splits on space and URLs don't contain space, so this alternate code works. I've also replaced do/done with braces:

websites="
    http://unix.stackexchange.com/
    http://stackoverflow.com/"

for i in $websites; {
    # works with /usr/bin/firefox
  /usr/bin/xdg-open "$i"
}
  • Thanks! Is there a way to just pass all my environment variables into the systemd script? I'd like these scripts to work even when I change my system (Different desktop environment etc.), so that I don't have to update these services every time something changes. – Joschua Jun 29 '16 at 15:33
  • No, because systemd is not run as a child process of your environment, so it doesn't inherit your environment. Systemd's explicit use of a minimal number of environment variables provides a consistent run time environment. – Mark Stosberg Jun 29 '16 at 17:26

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.