How to put a server to sleep on Linux?

There are two common methods to allow a server to shut down in a Linux system: setting up system services or using cron jobs.

  1. Configure system services.
  2. Create a new systemd service file, such as shutdown.service, with the following content:
    [Unit]
    Description=Shutdown the server

    [Service]
    Type=oneshot
    ExecStart=/sbin/shutdown -h now

    [Install]
    WantedBy=multi-user.target

  3. Save the file in the directory /etc/systemd/system.
  4. Enable the service and set it to start automatically on system boot:
    sudo systemctl enable shutdown.service
    sudo systemctl start shutdown.service
  5. Later, you can manually execute “sudo systemctl stop shutdown.service” to cancel the suspension operation.
  6. Utilize scheduled tasks:
  7. Edit the current user’s scheduled tasks using the crontab -e command.
  8. Add a command to schedule a shutdown operation at a specific time, such as: 0 0 * * 0 /sbin/shutdown -h now.
  9. Save and exit the editor.

After setting up through the above methods, the server will automatically shut down at the specified time, achieving the effect of hanging up.

bannerAds