G'day
BACKGROUND
We had a Member who has an unreliable internet connection ask elsewhere
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:
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)
Content is
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
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
(save and exit nano)
NOTE: if /home/yourusername/.config/ does not contain a folder autostart create it.
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.
Content is
Save and exit nano.
Then
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 @
Output is
Now may be a good time to test if our new service works.
and for me, it does. Firefox opens where I last left it.
To finish enter the following in Terminal
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.
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
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.
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

