Linux suspend at time

How to make a system suspend and resume on schedule?

I have a Linux server that’s only needed at workdays in work hours. There’s no point having it powered on all the time. How can I make a Linux system suspend and resume on set times and days without any supervision?

Basically, you are creating points of failure and more maintenance shutdowning down and booting it. The boot process might also use up more energy than leaving it on at night, I suspect. If you make a point of turning it off, I would do it only on weekends.

@Rui this is using suspend and resume, so the boot process is very short (there effectively isn’t one). I do something similar on a few systems, including some with hardware RAID etc. which take ages to boot normally, and resuming is instantaneous and doesn’t use much power at all.

2 Answers 2

I’ve created a shell script for this purpose using systemctl suspend and rtcwake commands to manage the power.

When run, it’ll work in a loop (15 minute cycles):

  • checking what time and day of week it is,
  • determining when it should wake up
  • checking if it should suspend right now
#!/bin/bash while [ 1=1 ]; do # repeat forever clear echo "Updated: $(date '+%F %T')" echo "### TIME SECTION" SLEEP_H=18 # What hour should the system suspend at? WAKE_H=8 # What hour should the system wake up at? # check what day of the week it is now DOW=$(date +%w) # 1 = monday, 5 = friday etc. echo "Day of week: $DOW" # check what time is it now H=$(date +%H) M=$(date +%M) NOW_S=$(date +%s) echo "Hour: $H, minute: $M" #date +%s --date "tomorrow 8:00" if [[ $DOW -lt 5 ]]; then echo "It's Monday to Thursday!" WAKE="tomorrow $WAKE_H:00" else echo "It's Friday to Sunday!" WAKE="monday $WAKE_H:00" fi WAKE_S="$(date --date "$WAKE" +%s)" WAKE_SR=$(( $WAKE_S - $NOW_S)) echo echo "Wake: $WAKE" echo "Wake relative seconds: $WAKE_SR" echo rtcwake -m no -s "$WAKE_SR" # sleep section sleep 15m echo "### SLEEP SECTION" if [[ $H -gt $SLEEP_H ]]; then # sleep if the hour is right systemctl suspend continue else echo "It's too early to sleep" fi if [[ $DOW -gt 5 ]]; then # sleep if it's saturday or sunday systemctl suspend continue else echo "It's not weekend - gotta stay up!" fi done 

This script is set to make the system wake up between Monday to Friday at 8 AM, and fall asleep at 6 PM.

Читайте также:  Linux and internet security

It’ll sleep during weekends and nights. Feel free to modify it to your needs.

I keep this script in /root/power-schedule.sh . Make sure it’s executable: `sudo chmod +x /root/power-schedule.sh’

Also put the script in crontab with the @reboot time to make sure that a sudden power outage will not interrupt your schedule.

@reboot exec /root/power-schedule.sh 

You might also want to configure the UEFI/BIOS so that the machine will power on when it detects AC power — this will cause it to boot up after a power outage (instead of staying off — which is usually the default behaviour).

Источник

How to boot, shut down, and suspend your system from the Linux command line

Learn how to schedule timed shutdowns and reboots with systemd and to hibernate your system with systemctl.

Sysadmin working on three monitors in dark room

Let’s face it: The most fundamental thing we do with computers is turn them on and off. Everything else happens between those two particular events. And sometimes, restarting the system is a key component of troubleshooting or completing installation processes.

Training & certification

Typically, you need root privileges to shut down or reboot the system. Ensure your account is configured in /etc/sudoers for this authority. Remember, it’s generally a bad idea to log on directly as root.

Boot the system

Starting the system is as simple as pressing the power button. What happens from there varies a bit depending on your hardware, but in general, the process looks like this:

  1. The firmware (BIOS or UEFI) finds the boot media.
  2. The boot loader starts, and the Master Boot Record (MBR) or GUID Partition Table (GPT) loads.
  3. The operating system selection menu appears.
  4. The boot loader stage 2 starts and loads the selected kernel.
  5. The kernel and drivers load and mount the root filesystem.
  6. systemd launches as PID 1.
  7. The default.target file loads.
  8. The user is prompted to authenticate.

The system launches to the default.target (providing either a command-line interface or graphical environment). Changing these targets is outside the scope of this article but is something I cover in How to switch between the CLI and GUI on a Linux server.

Shut down the system

When Red Hat Enterprise Linux (RHEL) adopted systemd with RHEL 7, what happened to the venerable Linux shutdown command? It remains—but now it maps to systemd’s shutdown functions.

The shutdown command features two options: —halt and —poweroff . The —halt option stops the operating system while the —poweroff option turns off the system.

One of the main benefits of the shutdown command is the ability to define a shutdown delay to give users time to save their work and log off the system. Schedule a time that suits your needs by using the format hh:mm (24-hour time designations).

Читайте также:  Filezilla linux как установить

For example, to halt the system at 10pm, type:

It’s more likely that you’ll want to halt the operating system a few minutes into the future. In that case, specify the number of minutes from now to begin the shutdown process.

For example, to halt the system after a five-minute delay, type:

You can append a message to all users by entering it after the time specification, like this:

$ sudo shutdown --halt +5 “Attention. The system is going down in five minutes.”

Cancel a timed shutdown by using the -c option:

IT Automation ebook

You can also use the systemctl command to shut down the system. For example, type systemctl halt or systemctl poweroff to achieve similar results to the shutdown command. The main disadvantage of using systemctl is losing the ability to schedule or cancel the shutdown process.

Reboot the system

When someone approaches you for help with their computer, I suspect one of your first responses is, «Did you reboot it?» Restarting a computer helps with various problems and may even be necessary to finalize some configurations. However, restarting is considered downtime on a server and should be avoided if possible. Try to use systemctl restart to restart services rather than reboot the whole system.

However, sometimes a restart is inevitable. The option for restarting the system immediately with the shutdown command is -r , so it looks like this:

You can still specify a delayed time using the hh:mm format explained above.

You can also use systemctl to reboot the device by typing:

Again, the disadvantage with systemctl is the inability to delay the process.

Suspend the system

Great Linux resources

There are a couple of ways to place the system in a reduced or no-power mode. The first is suspend and the second is hibernate.

When you suspend the system:

  • Contents of memory are moved to the swap location.
  • The boot loader is configured to boot directly to the current kernel.
  • The system shuts down (no-power mode).
  • Upon power up, the system reloads itself from swap.

When you hibernate the system:

  • Applications are stopped.
  • System state is moved to RAM.
  • The system remains powered on in a low-power state.

You can also suspend and hibernate the system by using the systemctl command. The commands are exactly what you’d expect:

$ sudo systemctl suspend $ sudo systemctl hibernate $ sudo systemctl hybrid-sleep

The systemctl hybrid-sleep command both suspends and hibernates the system.

Use the GUI

Depending on the graphical user interface (GUI) environment you have installed, you can initiate a reboot or shutdown from a menu. Many people rely on a graphical desktop like GNOME, so this is certainly a viable option.

Читайте также:  Основы linux от основателя gentoo все части

Power off and log out options in GNOME

[ Because sudo is something you don’t configure often, it can be hard to remember its nuances. Download the Linux sudo cheat sheet. ]

Wrap up

The familiar shutdown command now maps to systemd and executes timed shutdowns and reboots. You can use systemctl if you wish, but being able to schedule the shutdown is helpful. In addition, systemctl also offers suspend and hibernate options. Try to avoid bringing down the system when possible, but sometimes it’s necessary.

Источник

Sleep Timer to Suspend in Ubuntu MATE 16.04 LTS [duplicate]

If you don’t mind using a terminal, you can do it this way.

Execute : sleep 2h 45m 20s && systemctl suspend -i

It suspends your system in 2 hours, 45 minutes, 20 seconds.

I wrote a script for getting a GUI like this:

enter image description here

enter image description here

Copy the content of the script below and save it in your home directory as fast_suspend.sh .

#!/bin/bash # Tested on : Ubuntu 16.04 LTS # Date : 19-May-2016 # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. set -- `zenity --title="Scheduled-action" \ --forms \ --text='Enter Relative time' \ --add-entry="hours" \ --add-entry="min" -\ --add-entry="sec" \ --separator=".0 " \ --add-combo=action --combo-values="poweroff|restart|suspend|logout"` hrs=$1 min=$2 sec=$3 action=$4 time=$hrs\h\ $min\m\ $sec\s #Checking validity of the input : re='^8*([.]9+)?$' if ! [[ $hrs =~ $re ]] || ! [[ $min =~ $re ]] || ! [[ $sec =~ $re ]] then zenity --error \ --title="Invalid Input" \ --text="You have entered an Invalid time! \n\nOnly positive integers supported" exit 1 fi case $action in "poweroff") zenity --title=Confirm --question \ --text="Your system is about to $action in $ hours $ minutes $ seconds. \nAre you sure \?" if [ $? -eq 0 ] then sleep $time && poweroff else exit fi ;; "restart") zenity --title=Confirm --question \ --text="Your system is about to $action in $ hours $ minutes $ seconds. \nAre you sure \?" if [ $? -eq 0 ] then sleep $time && reboot else exit fi ;; "suspend") zenity --title=Confirm --question \ --text="Your system is about to $action in $ hours $ minutes $ seconds. \nAre you sure \?" if [ $? -eq 0 ] then sleep $time && systemctl suspend -i else exit fi ;; "logout") zenity --title=Confirm --question \ --text="Your system is about to $action in $ hours $ minutes $ seconds. \nAre you sure \?" if [ $? -eq 0 ] then sleep $time && gnome-session-quit --logout --no-prompt else exit fi ;; esac 

Additionally you can create a .desktop file to run it.

Copy the text below and save it as fast_suspend.desktop .

[Desktop Entry] Type=Application Terminal=false Icon= Name=fast_suspend Exec=/home/your-user-name/fast_suspend.sh Name[en_US]=fast_suspend 

Provide execution permission for both files — execute :

chmod a+x ~/fast_suspend.sh ~/Desktop/fast_suspend.desktop 

When you want to launch this window again, just double click on fast_suspend on the desktop.

Источник

Оцените статью
Adblock
detector