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.