How to re-connect quickly to internet when the laptop lid is lifted

I am running Linux Mint 22 on my Lenovo Thinkpad T470.

When I re-open the laptop lid it would take several minutes for internet to re-connect.

In order to solve this problem I consulted ChatGPT and was given a few options to try. I discovered that restarting the NetworkManager did the trick.

I then asked ChatGPT how to automate this process. It suggested setting up a service that would launch whenever the laptop lid is opened.

Here is how I implemented it:

1. Create a systemd service

You can create a custom systemd service that triggers when the laptop lid opens.

Open Terminal and create a new service file:

sudo vim /etc/systemd/system/lid-open.service

Add the following configuration:

[Unit]
Description=Run a command when the laptop lid is opened
After=suspend.target

[Service]
Type=oneshot
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=suspend.target

Replace /path/to/your/script.sh with the path to the script or command you want to run when the lid opens

2. Create the Script

Create the script that will be executed when the lid opens.

In your home directory, create the script file:

vim ~/lid-open-script.sh

Add your desired command to the script. For example, if you want to reconnect to Wi-Fi, your script might look like this:

#!/usr/bin/bash
sudo systemctl restart NetworkManager

Make the script executable

chmod +x ~/lid-open-script.sh

3. Enable and Start the Service

Enable and start the service so that it runs when the laptop resumes after the lid opens.

Enable the service:
sudo systemctl enable lid-open.service

Start the service:

sudo systemctl start lid-open.service

4. Test the Setup

Close and reopen your laptop lid to see if the script runs as expected. You can also check the status of your service to troubleshoot if necessary:

sudo systemctl status lid-open.service

This method allows you to automate the execution of any command or script when the laptop lid opens. If you run into any issues, you can check the logs with journalctl -xe for further details.

Wake up Linux Mint with PS/2 Mouse

Thanks to ChatGPT which gave me specific and accurate instructions I can now wake my computer running Linux Mint when the monitor goes to sleep (not Suspend) after the time specified in the Power Management settings.

Here is what ChatGPT told me to do:

To enable your Linux Mint system to wake up from suspend using a PS/2 mouse, you can try the following steps:

Check if the PS/2 mouse is recognized:

Open a terminal and run:

cat /proc/bus/input/devices

This command will list all input devices. Look for a section related to your PS/2 mouse. It might be labeled something like “ImPS/2 Generic Wheel Mouse” or similar.

Enable wakeup for the PS/2 mouse:

First, identify the device file corresponding to the PS/2 mouse. You can usually find it under /sys/bus/serio/devices/serioX/power/wakeup where X is the specific identifier of your mouse device.


Check if wakeup is enabled for the device by running:

cat /sys/bus/serio/devices/serioX/power/wakeup

Replace serioX with the correct identifier for your mouse. If it says “disabled,” you’ll need to enable it.


To enable it, run the following command (you’ll need root privileges):

echo enabled | sudo tee /sys/bus/serio/devices/serioX/power/wakeup

Make the change persistent:

To ensure that the setting persists after a reboot, you can add the command to a startup script.

Edit the file /etc/rc.local (create it if it doesn’t exist) and add the wakeup command just before the exit 0 line. For example:

# Enable wakeup for PS/2 mouse
echo enabled > /sys/bus/serio/devices/serioX/power/wakeup
exit 0

Make sure the file is executable:

sudo chmod +x /etc/rc.local

Test it out:

Suspend your system and then try moving the PS/2 mouse to see if it wakes up your system.

Lilypond for Presentation Slides

I have some lilypond files that need to be modified so that the text is larger so that they can be added to Presentation slides and still be readable.

The process requires a few different steps.

Change the font size as follows (vim substitute command)

s/huge/sans \\fontsize #3.8/

Modify the source file so that the number of lines of text will fit approximately on half of a half page.

Convert the pdf that is generated by Lilypond to a png or jpg file while keeping the quality.

convert \
-verbose \
-density 150 \
-trim \
test.pdf \
-quality 100 \
-flatten \
-sharpen 0x1.0 \
24-18.jpg

Fixing the Scanner Error after a new Install of Linux Mint

I have a Samsung M2885FW All in One Printer/Scanner.

When I install Linux Mint the scanner is automatically configured, and shows up in Scan jobs, but when a scan is initiated the error “unable to scan” “scanner not found” crops up.

The error seems to be related to the fact that the driver is listed as WSD (Web Services on Devices). It seems to be a driverless way to make printing simple and easy. However it is anything but that in my case.

I struggled to remember/recall the way this was fixed in the past, so I am recording it here with the hope that it will save a lot of time on the next install or upgrade of Linux Mint.

I disabled WSD on the printer setting interface that can be found at: https://LocalIPAddressOfPrinter/

Then I added the SULDR repository found at https://www.bchemnet.com/suldr/

Make sure that you follow the new standard for specifying the keyring. It needs to go into the /usr/share/keyrings/ directory and the deb entry in the additional-repositories.list file needs to have the signed-by attribute added as follows:

deb [signed-by=/usr/share/keyrings/suldr-keyring.gpg] https://www.bchemnet.com/suldr/ debian extra

Update the list of (installed) software, and then install the appropriate driver.

In my case it was suld-driver2-1.00.39 (or whatever the latest revision number).

Run apt install with the appropriate driver and then you should be good to go. Make sure that the scanner listed by “Document Scanner (aka simple-scan)” does not have a WSD designation attached to it.

Un-Chipmunking a Logitech Webcam Microphone

Just for fun I added an extra webcam to my desktop computer, thinking that it might be useful from time to time to add a different perspective to my video conferencing sessions etc.
However, I quickly discovered that the microphone on this new webcam was making me sound like Alvin of the Chipmunks. The simple solution was to switch to the microphone on the other webcam, but this became increasingly annoying. Also, I thought there must be a solution that would fix the problem once and for all.
Every problem creates an opportunity to dive deeper into the technology behind something. Get introduced to the world of webSDR and you end up learning about Autoplay policy in different browsers and exploring how to change the default policy programmatically. All because you had the idea to schedule recordings of shortwave broadcasts instead of juggling your calendar in order to be there at the proper time.
However, I digress.
Back to my squeaky microphone.
It turns out that you get to learn about sampling rates and other arcane tech trivia.
Once you hit the right results in your Google searches, the fix was relatively easy. All you need to do is change a setting in a Pulseaudio configuration file.(On a Linux machine, that is!)
In the process you learn about a new command line command, learn how to identify your sound devices (such as microphones) and modify the appropriate config file to fix the problem.
Voilà no more Alvin and lots more Peter.
Here are the details:
Apparently, Logitech webcams have a tendency to get configured with a sampling rate of 48000, which is three times too much. So you need to get a device name for the offending microphone and then change the configuration so that it is registered at the proper 16000 instead.
On my Linux machine I did the following, thanks to this helpful post:

https://evgenykuznetsov.org/en/posts/2020/webcam-sound/

What I learned:

A new command to list audio devices:

pacmd list-sources

Identifying the desired device from the output of the above command, and modifying the appropriate config file:

/etc/pulse/default.pa

to add a line to set the proper sampling rate:

load-module module-remap-source master=<device name from above> rate=16000

Get interface names

I remember something on Stack Overflow or one of the other similar technical help forums that showed a way to get all the interface names on a particular computer. I could not find the reference and my attempts to recall it from memory were not successful, so I wrote the following command line that seems to do the trick:

for i in $(ip -o link show | cut -d: -f2 | xargs); do if [[ $i != "lo" ]]; then echo $i; fi; done

Explanation: use the ip command using the “one line” option to list all the network interfaces. They will be the second field on each line of the resulting output. The xargs command will trim leading and trailing spaces from the interface name. Filter out the loopback interface “lo” and then echo each entry.

Mounting a VirtualBox Hard Drive image in Linux

I was looking for a way to access files on a Virtual Box hard drive. As usual StackExchange came to the rescue. Here is how I mounted a FreeDos virtual hard drive and gained access to the files that were on it:

sudo modprobe nbd
sudo qemu-nbd -r -c /dev/nbd1 /home/peter/VirtualBox\ VMs/Knoppix/FreeDos/NewVirtualDisk1.vmdk
sudo mount /dev/nbd1p1 /media/peter/vmdk/

Now the hard drive files are available on /media/peter/vmdk/

When done:
sudo umount /media/peter/vmdk

Solving “Hang” on Dell Laptop running Linux Mint

I found this workaround and applied it to my Dell Inspiron 3551 (purchased with Ubuntu pre-loaded, but currently running Linux Mint 19.1 (Tessa)

Source: https://easylinuxtipsproject.blogspot.com/p/bugs.html#ID25

Random freezes on Intel-based computers

25. On certain hardware combinations with Intel CPU’s, you can experience random freezes of your system. In particular the Bay Trail series of Intel CPU’s is known to be affected by this.

(The Dell Inspiron 3551 fits this description exactly.)


This can often be solved by sharply reducing the maximum sleep state of the CPU when idle (maximal C-state). Namely from 6 (or 9) to 1.

The price you pay for that is some extra power consumption, which is of course rather annoying for laptops (you’ll notice a somewhat higher battery drain speed). But at least your machine should run stable now.

Proceed like this:

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. First check your current max C-state. Use copy/paste to transfer the following command line into the terminal:

cat /sys/module/intel_idle/parameters/max_cstate

Press Enter.

The output will probably be 6 or 9.

c. Now copy/paste the following line into the terminal, in order to edit a settings file:

xed admin:///etc/default/grub

Press Enter.

d. Delete this existing line:

GRUB_CMDLINE_LINUX=””

Replace it by this line (use copy/paste to transfer it):

GRUB_CMDLINE_LINUX=”intel_idle.max_cstate=1″

e. Save the changes and close the modified file.

f. Then execute this command line in the terminal:

sudo update-grub

g. Reboot.

h. Check the change with this command:

cat /sys/module/intel_idle/parameters/max_cstate

The output should be 1 now.

Live Edit of MariaDB generated table using Jquery and Bootstrap

My latest research involved the generation of a web table from a MySql/MariaDB database, with live CRUD capability (Create, Read, Update, Delete).
Key Learnings: JQuery is your friend. Bootstrap is also your friend. W3Schools is your friend. StackOverflow is your friend. Github is your friend.

With the help of all the above mentioned resources I was able to take a database of information, list it in a web page and then have options to delete records, update records, and add records. The biggest headache came with the question of how to signal a change to existing information in the listed table. An example I found on Github used the .on(blur…) functionality of JQuery but it had a bug(?) associated with it when the blur event fired multiple (an infinite number?) times. I solved the problem unsatisfactorily by changing .on(‘blur’,…) to .on(‘dblclick’…). I wanted to try avoiding mouse operations, but for now this has fixed the issue.

Update: April 1, 2023 (and this is not an April Fool)

The latest version of JQuery appears to have fixed this issue. The ‘dblclick’ references were changed to ‘blur’ references and the page appears to be working as intended.

This example on Github was particularly helpful:

https://github.com/rahmat058/Live-Table-Add-Edit-Delete-using-Ajax-Jquery-in-PHP-Mysql

[P]ersonal[A]udio[R]ecorder – Part Two

I’ve only done an elementary amount of BASH scripting, so as a learning tool I decided to automate even further, the process I described in Part One. Many streaming URLs are very long and complicated, so it makes sense to collect them and then make them accessible for automated “copy” and “paste”.

We live in a country of different time zones and therefore it is very convenient to be able to “listen” to the radio (especially the Canadian Broadcasting Corporation or CBC) in a different region at a different time. This can be used to hear something that was missed the first time, or to listen “ahead” to something when it is first aired.

So my desire to automate the “stream recording” process led me to find a list of CBC streaming URLs. Having done this I then compiled a list of places and stream URLs and put them in a text file in the form:
line1: place (in quotes)
line2: url (unquoted)

This can then be read into a Bash Array and converted to a Bash Associative Array so that a URL can be associated with a place or radio station name.

I learned a lot putting this script together, and it took a maddening amount of debugging as I learned some of the idiosyncrasies of Bash syntax. In particular I learned that quotes and/or the lack of them can lead to great frustration and disbelief.

In the process I also learned that processing command options and arguments is a non-trivial exercise. Fortunately I discovered argbash and despite some initial confusion about the “argbash process” I plugged on and learned how to use it.

The script I ended up writing is certainly not bomb-proof, but it suits my purpose pretty well and it can easily be used to schedule audio recordings of one minute to many minutes (up to the limit of available disk space!) of recording from any one of forty-two different radio streams. This list can certainly be expanded. There are literally hundreds, if not thousands, of radio streams available. For now I am interested in the CBC streams, along with a local internet radio station called “Cabin Radio”.

I also learned how to use “zenity” to prompt an interactive user of the “stream_record” script with pretty Gtk dialogue boxes.