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