Score:0

How do you make a script that will automatically shutdown all the services running on my home server properly when the power goes out?

id flag

I am new to Linux and I am having a lot of fun with Ubuntu 20.04.2 installed on a very old MacBook Pro. Currently my little home server has a Samba share to share some movies and the like. It has a vnc server installed so that I can access the GUI as I am not fully comfortable with pure command line stuff just yet. The server also runs an instance of Pi Hole to block ads on our local network. I know how to shutdown each individual service that is running manually from the command line, however I would like to automate this for when the power cuts out. Since my Ubuntu is installed on an old laptop, I essentially have a built in UPS. How would I make a script or something similar that can run in the background and monitor whether or not the laptop is plugged in and then when the laptop is unplugged for a certain amount of time, it begins to shutdown all the running processes the correct way? Then on the flip side, how would I make all those services start up again once I manually turn the laptop back on? Any help would be much appreciated!

in flag
Based on what you’ve written, you’re running Ubuntu Desktop. This will make the task much easier. All you need to do is go into your power settings and choose when the notebook powers itself down while on battery. When you start the machine, all enabled services are restarted, so it’s a non-issue.
cn flag
There should not be a need to shutdown services manually if the shutdown command is used to shut the system down. That action already does a clean shutdown of services. So I agree with matigo: setting the behaviour for shutdown on battery could be enough.
Rory Schram avatar
id flag
Thank you to both of you for answering, this helps a lot!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.