3

I need to start firefox-esr once the network is available.

Here's what I've got:

sudo tee "/etc/network/if-up.d/upif" > /dev/null << EOFBOOT
#!/bin/bash
DISPLAY=":0"
if [[ "\$IFACE" = "eth0" || "\$IFACE" = "wlan0" ]]; then
    echo "\$(date) \$IFACE" >> /var/log/upif
    firefox-esr &
fi
EOFBOOT
sudo chmod +x "/etc/network/if-up.d/upif"

I am getting the date and the interface name in my log file, which means the script runs, but firefox-esr doesn't start.

What I've tried:

  • adding DISPLAY=":0"
  • grep -inR "firefox" /var/log
  • using full path to executable, i.e. /usr/bin/firefox-esr
  • using sudo firefox-esr &
  • removing the trailing &

Nothing helped. However, if I put IFACE="eth0" below the shebang and then run sudo /etc/network/if-up.d/upif manually, that exact script you see above does the job and starts firefox.

I am not sure what's the problem there.

This is 2018-06-27-raspbian-stretch-lite running Debian 9.4.

P.S. Also I initially had logic checking whether Firefox already runs before attempting to start it, but once I realised Firefox isn't launched at all, I removed it

UPDATE Found a similar problem: Start a GUI in NetworkManager if-up.d script

2 Answers 2

3

The network event scripts run as the root user, but your desktop environment probably (hopefully) isn't.

This seems like a problematic approach in general. A few things are missing:

  • Firefox should run as your logged in user, not root.
  • Firefox needs to be able to authenticate to the X server to connect to it (see ~/.Xauthority and man xauth).
  • Firefox's environment needs to be set up to work properly in the desktop session (HOME, XDG_RUNTIME_DIR etc.)

I would suggest trying a different approach, e.g. having the network script communicate network status (via a pipe / temporary file / DBus) to something already running in the user session (launched e.g. from ~/.xinitrc), which then launches GUI apps as needed.

2
  • Thank you for your answer. I've managed to make it the initial approach work - turned out to be easier than I thought it would be. I've shared what helped and will mark that as an answer. Upvoted your answer to appreciate the effort and a quick response. Thanks again!
    – mehov
    Jul 27, 2018 at 7:38
  • UPDATE: this is what I ended up doing - checking internet availability in a script every minute. Accepting. Thanks again.
    – mehov
    Aug 13, 2018 at 13:38
0

Thanks to Fadabi Nawi, here's what the answer was:

su pi -c "DISPLAY=:0.0 /usr/bin/firefox-esr &"

Key points:

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.