Your Browser On Demand - A Couple Of Options - Wizards Corner

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
12,637
Reaction score
12,335
Credits
65,291
G'day

BACKGROUND

We had a Member who has an unreliable internet connection ask elsewhere

  • Whether there is a method that will check for an active connection, and if so, launch Firefox
  • Whether there is a method that will check, following resume from suspend, for an active connection, and if so, launch Firefox

The answer in both cases is Yes.

The Member is using Ubuntu MATE, I have a 22.04 in my stable so I initially used that as a testing environment.

DEGREE OF DIFFICULTY

I would put this at Intermediate, but a Beginner who pays attention may follow it.

PREPARATION

You will need the following to achieve what I have done:
  • 1 x Bash Script, with content similar to mine
  • 1 x .desktop file, similar to mine, and
  • 1 x Systemd Service, user written, with a view to enabling and starting it.

You need a Linux Distribution that runs Systemd, which includes but is not limited to

Ubuntu - all desktops
Linux Mint, both Ubuntu-based and Debian-based
Debian and its other "cousins"

This also works with Fedora and Arch, but I will deal with those later.

SYSTEM RECOVERY

As with many operations that may change the status of your operating system, you should have a current backup or Timeshift snapshot available to you, should you need it.

STEPS FOR

1 x Bash Script


You can name your script as you wish, but it is bad practice to give it the same name as an existing command, so don't call it firefox. I have named mine fireup.sh.

1. In your terminal emulator (Terminal, Konsole)

Code:
sudo touch /usr/local/bin/fireup.sh

sudo nano /usr/local/bin/fireup.sh

Content is

Code:
#!/bin/bash

# Define a reliable host to ping
HOST="google.com"

# Number of pings to send
COUNT=1

# Check for internet connectivity using ping
if ping -c "$COUNT" "$HOST" &> /dev/null; then
    echo "Internet connection detected. Launching Firefox..."

export DISPLAY=:0.0
firefox

else
    echo "No internet connection detected."
fi

To exit out of Nano

Press Ctrl-x
y or Y for Yes (at modified buffer)
(the filename will be shown, if correct) Enter

Then make the script executable

Code:
sudo chmod +x /usr/local/bin/fireup.sh


1 x .desktop file

This to launch firefox when you reach your desktop, it has a delay in it, I have given mine 20 seconds, you may need to tweak yours.

I am aware that the Member has remapped his Tilde key to perform another function, so he should start with a full path to the file. Others can use ~/.config as part of the path, he should use /home/his-username-here/.config/


Home typically does not require the use of sudo.

Mine has content as follows

Code:
touch /home/chris/.config/autostart/myfire.desktop

nano /home/chris/.config/autostart/myfire.desktop

Code:
[Desktop Entry]
Name=Firefox Web Browser
Keywords=Internet;WWW;Browser;Web;Explorer
Exec=/usr/local/bin/fireup.sh
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=firefox

(save and exit nano)

NOTE: if /home/yourusername/.config/ does not contain a folder autostart create it.

Code:
mkdir /home/yourusername/.config/autostart/

and create the file there.

NOTE:

At this point it may be a good idea to see that what we have done works, so reboot your computer and see if Firefox launches at the Desktop.

Once you have established that the .desktop file and script work, close down your Firefox and we will move on to building the Systemd service.

1 x Systemd Service, user written

I am not going to go into detail here on Systemd, you can read up on that for yourself, but if you are using Ubuntu, Linux Mint, stable Debian, Fedora, most Arch, and so on, you are using it.

Briefly from Wikipedia and AI in part

systemd is a software suite for system and service management on Linux built to unify service configuration and behavior across Linux distributions. Its main component is an init system used to bootstrap user space and manage user processes. It also provides replacements for various daemons and utilities, including device management, login management, network connection management, and event logging.

and

systemd is a core software suite that acts as both a system and service manager, responsible for booting the system, managing processes, and providing a unified interface for handling services, device management, and other system tasks. It's designed to be an advanced, parallel-processing successor to the traditional SysVinit system, organizing components into "unit" files for flexible and efficient operation.

Services, targets and units all form a part of the terms associated with systemd.

I provide here one solution for having Firefox launch once you resume from a suspend state, via having the following user-written Service run at that time.

Best practice is to preface building and enabling our service with a reload of running services (daemons), and conclude with that repeated.

I am naming my service as resume_fire. Since, in contrast to existing service, this is user-written, it needs to have an @ at the end of the name part. Further, we will see that, once I seek to enable and start it running, I have to apply my username (chris, for me) following the @

I will store my service in /etc/systemd/system/ ... although there are other areas where services can exist.

Code:
sudo touch /etc/systemd/system/[email protected]
sudo nano /etc/systemd/system/[email protected]

Content is

Code:
[Unit]
Description=Run my fireup script after 20s delay
After=sleep.target

[Service]
User=%I
Type=simple
ExecStartPre=/usr/bin/sleep 20
ExecStart=/usr/local/bin/fireup.sh

[Install]
WantedBy=sleep.target

Save and exit nano.

Then

Code:
sudo systemctl daemon-reload
sudo systemctl enable [email protected]

This will generate an error because this is a non-core service (user-written), which you will remember after a couple of times, lol.

Error output is

Failed to enable unit: File sleep.target: Identifier removed

Repeat the command, this time with your username following the @

Code:
sudo systemctl enable [email protected]

Output is

Code:
Created symlink /etc/systemd/system/sleep.target.wants/[email protected] → /etc/systemd/system/[email protected].

Now may be a good time to test if our new service works.

Code:
sudo systemctl start [email protected]

and for me, it does. Firefox opens where I last left it.

To finish enter the following in Terminal

Code:
sudo systemctl daemon-reload

Now your service will operate each time that you reboot.

If I open my File Manager (Caja in MATE) and check, I will find that /etc/systemd/system/ has [email protected] in it, and that /etc/systemd/system/sleep.target.wants has in it [email protected]

I should say that the use of "sleep" in the service is because sleep, suspend, hibernate, hybrid-sleep and so on, are all covered with the use of sleep.

You can now shut down your Firefox, suspend your computer, give that maybe 30 seconds, then resume, and see that Firefox launches.

If you do not close down Firefox first before suspending, the process will work, but in a number of cases you may get a non-fatal popup error that you can dismiss to go on.

oKguSUY.png


The proof of the pudding is in the eating, and following is a short video I took today of the end stages - suspending and resuming.

Viewing time 3 minutes 20 seconds.


As you might gather from the video, I might tweak the sleep time before launching Firefox to 25 seconds or 30 seconds to clear from those popup connection notifications. YMMV.

In subsequent Posts, as well as fielding any questions from the above, I will deal with other distros such as Fedora and Arch.

If you have a question which is lengthy, or diverges from the main content of this thread, start a new thread yourself and link to this one.

DISCLAIMER

There are many ways to get the same or similar outcome to the above, under Linux and this is no exception eg you could have an entry in your Settings to autostart a script or App, use cron or anacron, or even pm-utils perhaps. Mine is simply giving you ideas.

HTH

Chris Turner
wizardfromoz
 


@wizardfromoz :-

Nicely done, Chris. And well 'laid-out'. Even noobs should be able to follow that.

I frequently suspend overnight. I don't bother shutting the browser down; I simply leave everything as it is, and hit my custom suspend script, since my browsers are all configured to re-start exactly as they shut down anyway.

In all honesty, my personal feeling here is that checking for an active connection is overkill. If there isn't an active connection, the browser will very soon let you know in any case, but.....we all have different needs and/or requirements - and differing degrees of "fussiness"! - and in Linux, it's more or less a guarantee that you can usually script something that'll do what ya want.

This is yet another demonstration of exactly that.
good.gif



Mike. ;)
 
Just as an aside, I discovered the other day that Chrome has a 'mini-game' built in to the page it serves up when your internet connection has dropped! Anyone who's familiar with this will know the page depicts a tiny T-Rex dinosaur standing in a desert landscape.....complete with assorted stands of cacti.

It appears that by hitting the button now provided, the 'desert scene' starts scrolling right-to-left across the screen.....and you have to use the space bar to make T-Rex jump over the cacti. There's a running, real-time scoreboard up top, and when T-Rex fails to clear a stand of cacti, the game is over...

It's supposed to help relieve the annoyance & frustration some folks experience when this sort of thing occasionally happens..!!


Mike. :D
 
Thanks for the chuckle, Mike.

You might have been reading my mind, as I was returning here today to post the following:

DIFFERENT BROWSERS

If Firefox standard is not your browser of choice, you can use the material in #1. Just substitute instances of firefox with commands such as
  • Firefox-ESR - firefox-esr
  • Brave - brave
  • Chrome - will be one of google-chrome or google-chrome-stable
  • Chromium - will be one of chromium or chromium-browser
  • Librewolf - librewolf
  • and so on
  • and, dare I say it, Microsoft Edge for Linux works too.
If you like, rename your Systemd service from resume_fire to eg resume-browser, and rerun your systemctl commands.

EDIT - forgot Waterfox, which Brian @Condobloke and I use.

If you downloaded your Waterfox from Alex Kontos's website, it is likely a .tar.bz2 archive that you may have chosen to Extract Here, in ~/Downloads/ , generating a folder Waterfox. The file needed is waterfox-bin.

So use the full path to the command, in my case that would be

/home/chris/Downloads/waterfox/waterfox-bin

Wiz
 
Last edited:
@wizardfromoz :-

Just remembered this:-

For anybody who's interested, Chris - and is running a Chromium-based browser (with the exception of Opera) - the 'game' can be called up from the URL / search 'combobox' with:-

Code:
chrome://dino/

No need to wait for your internet connection to go down!

Mike. ;)
 
Last edited:
Great tutorial friend!- :)

I've had zero problems with Waterfox over the last 5 weeks!
 
There is a package in Debian that checks connectivity.
network-manager-config-connectivity-debian
Code:
This package contains a configuration file which enables NetworkManager's
connectivity checking functionality.

NetworkManager will try to connect to a Debian server to determine the
connection status.

This is particularly useful for captive portal detection.

No user data is transmitted in the connectivity checks, but merely contacting
the Debian connectivity check servers reveals that the user is running a
Debian(-based) operating system with NetworkManager.
 
G'day

BACKGROUND

We had a Member who has an unreliable internet connection ask elsewhere

  • Whether there is a method that will check for an active connection, and if so, launch Firefox
  • Whether there is a method that will check, following resume from suspend, for an active connection, and if so, launch Firefox

The answer in both cases is Yes.

The Member is using Ubuntu MATE, I have a 22.04 in my stable so I initially used that as a testing environment.

DEGREE OF DIFFICULTY

I would put this at Intermediate, but a Beginner who pays attention may follow it.

PREPARATION

You will need the following to achieve what I have done:
  • 1 x Bash Script, with content similar to mine
  • 1 x .desktop file, similar to mine, and
  • 1 x Systemd Service, user written, with a view to enabling and starting it.

You need a Linux Distribution that runs Systemd, which includes but is not limited to

Ubuntu - all desktops
Linux Mint, both Ubuntu-based and Debian-based
Debian and its other "cousins"

This also works with Fedora and Arch, but I will deal with those later.

SYSTEM RECOVERY

As with many operations that may change the status of your operating system, you should have a current backup or Timeshift snapshot available to you, should you need it.

STEPS FOR

1 x Bash Script


You can name your script as you wish, but it is bad practice to give it the same name as an existing command, so don't call it firefox. I have named mine fireup.sh.

1. In your terminal emulator (Terminal, Konsole)

Code:
sudo touch /usr/local/bin/fireup.sh

sudo nano /usr/local/bin/fireup.sh

Content is

Code:
#!/bin/bash

# Define a reliable host to ping
HOST="google.com"

# Number of pings to send
COUNT=1

# Check for internet connectivity using ping
if ping -c "$COUNT" "$HOST" &> /dev/null; then
    echo "Internet connection detected. Launching Firefox..."

export DISPLAY=:0.0
firefox

else
    echo "No internet connection detected."
fi

To exit out of Nano

Press Ctrl-x
y or Y for Yes (at modified buffer)
(the filename will be shown, if correct) Enter

Then make the script executable

Code:
sudo chmod +x /usr/local/bin/fireup.sh


1 x .desktop file

This to launch firefox when you reach your desktop, it has a delay in it, I have given mine 20 seconds, you may need to tweak yours.

I am aware that the Member has remapped his Tilde key to perform another function, so he should start with a full path to the file. Others can use ~/.config as part of the path, he should use /home/his-username-here/.config/


Home typically does not require the use of sudo.

Mine has content as follows

Code:
touch /home/chris/.config/autostart/myfire.desktop

nano /home/chris/.config/autostart/myfire.desktop

Code:
[Desktop Entry]
Name=Firefox Web Browser
Keywords=Internet;WWW;Browser;Web;Explorer
Exec=/usr/local/bin/fireup.sh
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=firefox

(save and exit nano)

NOTE: if /home/yourusername/.config/ does not contain a folder autostart create it.

Code:
mkdir /home/yourusername/.config/autostart/

and create the file there.

NOTE:

At this point it may be a good idea to see that what we have done works, so reboot your computer and see if Firefox launches at the Desktop.

Once you have established that the .desktop file and script work, close down your Firefox and we will move on to building the Systemd service.

1 x Systemd Service, user written

I am not going to go into detail here on Systemd, you can read up on that for yourself, but if you are using Ubuntu, Linux Mint, stable Debian, Fedora, most Arch, and so on, you are using it.

Briefly from Wikipedia and AI in part



and



Services, targets and units all form a part of the terms associated with systemd.

I provide here one solution for having Firefox launch once you resume from a suspend state, via having the following user-written Service run at that time.

Best practice is to preface building and enabling our service with a reload of running services (daemons), and conclude with that repeated.

I am naming my service as resume_fire. Since, in contrast to existing service, this is user-written, it needs to have an @ at the end of the name part. Further, we will see that, once I seek to enable and start it running, I have to apply my username (chris, for me) following the @

I will store my service in /etc/systemd/system/ ... although there are other areas where services can exist.

Code:
sudo touch /etc/systemd/system/[email protected]
sudo nano /etc/systemd/system/[email protected]

Content is

Code:
[Unit]
Description=Run my fireup script after 20s delay
After=sleep.target

[Service]
User=%I
Type=simple
ExecStartPre=/usr/bin/sleep 20
ExecStart=/usr/local/bin/fireup.sh

[Install]
WantedBy=sleep.target

Save and exit nano.

Then

Code:
sudo systemctl daemon-reload
sudo systemctl enable [email protected]

This will generate an error because this is a non-core service (user-written), which you will remember after a couple of times, lol.

Error output is

Failed to enable unit: File sleep.target: Identifier removed

Repeat the command, this time with your username following the @

Code:
sudo systemctl enable [email protected]

Output is

Code:
Created symlink /etc/systemd/system/sleep.target.wants/[email protected] → /etc/systemd/system/[email protected].

Now may be a good time to test if our new service works.

Code:
sudo systemctl start [email protected]

and for me, it does. Firefox opens where I last left it.

To finish enter the following in Terminal

Code:
sudo systemctl daemon-reload

Now your service will operate each time that you reboot.

If I open my File Manager (Caja in MATE) and check, I will find that /etc/systemd/system/ has [email protected] in it, and that /etc/systemd/system/sleep.target.wants has in it [email protected]

I should say that the use of "sleep" in the service is because sleep, suspend, hibernate, hybrid-sleep and so on, are all covered with the use of sleep.

You can now shut down your Firefox, suspend your computer, give that maybe 30 seconds, then resume, and see that Firefox launches.

If you do not close down Firefox first before suspending, the process will work, but in a number of cases you may get a non-fatal popup error that you can dismiss to go on.

oKguSUY.png


The proof of the pudding is in the eating, and following is a short video I took today of the end stages - suspending and resuming.

Viewing time 3 minutes 20 seconds.


As you might gather from the video, I might tweak the sleep time before launching Firefox to 25 seconds or 30 seconds to clear from those popup connection notifications. YMMV.

In subsequent Posts, as well as fielding any questions from the above, I will deal with other distros such as Fedora and Arch.

If you have a question which is lengthy, or diverges from the main content of this thread, start a new thread yourself and link to this one.

DISCLAIMER

There are many ways to get the same or similar outcome to the above, under Linux and this is no exception eg you could have an entry in your Settings to autostart a script or App, use cron or anacron, or even pm-utils perhaps. Mine is simply giving you ideas.

HTH

Chris Turner
wizardfromoz
Good job. I am impressed. :-)

Your script works.

But how do I get it to work when I return from a suspended state ?
 
How can I get your script to work after my computer returns from a sleep state?

This may help.

It performs an action BEFORE my computer is suspended.

I am a retired chemist. I love learning new things. :-)

#!/bin/sh
# Backup files to maxtor drive before the computer has been suspended
# Put this in /lib/systemd/system-sleep/

case "$1" in
pre)
logger Files being backed up to Maxtor drive.
/home/andy/bin/Backup_Ubuntu_Mate.sh
logger done with backup, going to sleep.
;;
post)
logger waking up from sleep.
;;
esac
 
Did you follow the steps I outlined, where it begins

1 x Systemd Service, user written

??

That does just that.

It is reflected, also, in the video.

Wiz
 


Follow Linux.org

Members online


Top