How to start/stop network manager during suspend?

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
jefferson9
Level 1
Level 1
Posts: 5
Joined: Fri Dec 09, 2016 11:38 pm

How to start/stop network manager during suspend?

Post by jefferson9 » Sat Dec 10, 2016 4:04 pm

I recently installed mint 18 cinnamon, my system was taking a long (5-10 seconds) to suspend.

I discovered by running

Code: Select all

systemctl stop NetworkManager
it would completely fix the delay and suspend instantly. Then

Code: Select all

systemctl start NetworkManager
is necessary after waking from suspend for wifi.

How can I run these scripts automatically before and after suspend?

JeremyB
Level 20
Level 20
Posts: 11138
Joined: Fri Feb 21, 2014 8:17 am

Re: How to start/stop network manager during suspend?

Post by JeremyB » Sat Dec 10, 2016 5:00 pm

Based on the work by 1fallen here

We can make some systemd service files to do this for you

Code: Select all

sudo -H xed /etc/systemd/system/wifi-sleep.service
Paste the following into the empty file

Code: Select all

[Unit]
Description=Stop networkmanager before sleep
Before=suspend.target
Before=hibernate.target
Before=hybrid-sleep.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl stop network-manager.service

[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
Then enable with

Code: Select all

systemctl enable  wifi-sleep.service
You will likely get prompted for your password twice

Test it to see if it goes into sleep/suspend faster before we do the second service file

jefferson9
Level 1
Level 1
Posts: 5
Joined: Fri Dec 09, 2016 11:38 pm

Re: How to start/stop network manager during suspend?

Post by jefferson9 » Sat Dec 10, 2016 5:26 pm

JeremyB wrote:Based on the work by 1fallen here

We can make some systemd service files to do this for you

Code: Select all

sudo -H xed /etc/systemd/system/wifi-sleep.service
Paste the following into the empty file

Code: Select all

[Unit]
Description=Stop networkmanager before sleep
Before=suspend.target
Before=hibernate.target
Before=hybrid-sleep.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl stop network-manager.service

[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
Then enable with

Code: Select all

systemctl enable  wifi-sleep.service
You will likely get prompted for your password twice

Test it to see if it goes into sleep/suspend faster before we do the second service file
Thanks for the reply
It appears suspending is still slow. Although it does seem to disable the network manager successfully as I have no wifi after resuming.

JeremyB
Level 20
Level 20
Posts: 11138
Joined: Fri Feb 21, 2014 8:17 am

Re: How to start/stop network manager during suspend?

Post by JeremyB » Sat Dec 10, 2016 5:32 pm

I tested to see if it would stop the network manager service and that was my only idea and since it didn't work

Code: Select all

systemctl disable  wifi-sleep.service
But the file can be modified so it could enable the service after suspend/sleep so you would just have to manually stop it

Code: Select all

sudo -H xed /etc/systemd/system/wifi-resume.service
Enter

Code: Select all

[Unit]
Description=Enable networkmanager after sleep
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl start network-manager.service

[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target

Code: Select all

systemctl enable  wifi-resume.service

jefferson9
Level 1
Level 1
Posts: 5
Joined: Fri Dec 09, 2016 11:38 pm

Re: How to start/stop network manager during suspend?

Post by jefferson9 » Sat Dec 10, 2016 5:42 pm

JeremyB wrote:I tested to see if it would stop the network manager service and that was my only idea and since it didn't work

Code: Select all

systemctl disable  wifi-sleep.service
But the file can be modified so it could enable the service after suspend/sleep so you would just have to manually stop it

Code: Select all

sudo -H xed /etc/systemd/system/wifi-resume.service
Enter

Code: Select all

[Unit]
Description=Enable networkmanager after sleep
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl start network-manager.service

[Install]
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target

Code: Select all

systemctl enable  wifi-resume.service
Yeah this script enables the wifi after resuming. But for some reason it's still taking too long to suspend. If I execute the stop network manager cmd via terminal, then sleep, it sleeps instantly. Any idea why it would work via terminal but not with the service?

JeremyB
Level 20
Level 20
Posts: 11138
Joined: Fri Feb 21, 2014 8:17 am

Re: How to start/stop network manager during suspend?

Post by JeremyB » Sat Dec 10, 2016 5:56 pm

I would guess that systemd stops most services before sleep on its own and enough time passes between entering the command in terminal and closing the lid that sleep happens faster. That would also explain why my service attempt failed

jefferson9
Level 1
Level 1
Posts: 5
Joined: Fri Dec 09, 2016 11:38 pm

Re: How to start/stop network manager during suspend?

Post by jefferson9 » Sat Dec 10, 2016 7:39 pm

JeremyB wrote:I would guess that systemd stops most services before sleep on its own and enough time passes between entering the command in terminal and closing the lid that sleep happens faster. That would also explain why my service attempt failed
I think I may have found a fix,

Code: Select all

/lib/systemd/system-sleep/wpasupplicant
calls an executable

Code: Select all

/sbin/wpa_cli
with either suspend or resume parameter. I revoked executable permission on it and it now sleeps instantly, and wifi works (assuming because of those wifi-sleep/resume services I just created?)

Anyway hope this doesn't break anything else, it doesn't seem to so far. Anyway thanks, the whole systemd thing is very new to me.

User avatar
Minterator
Level 5
Level 5
Posts: 596
Joined: Thu Jan 10, 2013 8:29 am

Re: How to start/stop network manager during suspend?

Post by Minterator » Wed Aug 02, 2017 4:08 pm

jefferson9 wrote:
JeremyB wrote:I would guess that systemd stops most services before sleep on its own and enough time passes between entering the command in terminal and closing the lid that sleep happens faster. That would also explain why my service attempt failed
I think I may have found a fix,

Code: Select all

/lib/systemd/system-sleep/wpasupplicant
calls an executable

Code: Select all

/sbin/wpa_cli
with either suspend or resume parameter. I revoked executable permission on it and it now sleeps instantly, and wifi works (assuming because of those wifi-sleep/resume services I just created?)

Anyway hope this doesn't break anything else, it doesn't seem to so far. Anyway thanks, the whole systemd thing is very new to me.

Good catch! I'm surprised nobody else has complained about this. My LM 18.2 MATE on Kabylake hardware waits 10+ seconds to suspend. Revoking executable permission on "/lib/systemd/system-sleep/wpasupplicant" fixes that. And a bug report here:
https://bugs.debian.org/cgi-bin/bugrepo ... bug=835648

The title for this thread is misleading at this point, it should be: "Why does my computer wait 10+ seconds to suspend?"
Mint 17.3 MATE, kernel 4.11.12

Post Reply

Return to “Newbie Questions”