shutdown - Linux


Overview

The shutdown command in Linux allows you to safely halt, power-off, or reboot your system. It sends a signal to all users and processes, notifying them that the system is going down, and terminates processes gracefully.

Syntax

The basic syntax of the shutdown command is:

shutdown [OPTIONS] [TIME] [MESSAGE]
  • OPTIONS are the command flags that specify the type of shutdown operation.
  • TIME is the time at which the shutdown will take place. It can be specified as now (for immediate shutdown), +m (where m is the number of minutes), or hh:mm (time on a 24-hour clock).
  • MESSAGE is an optional parameter where a custom message can be broadcast to all users.

Options/Flags

  • -r: Reboots the system after shutdown.
  • -h: Halts the system without powering off.
  • -P: Powers off the system (default behavior).
  • -c: Cancels a scheduled shutdown.
  • -k: Only sends a warning message to all logged-in users, without actual shutdown.
  • -t SECONDS: Specifies a delay between sending the SIGTERM and SIGKILL signals to processes, allowing them some time to terminate properly.

Example:

shutdown -r +10 "Rebooting in 10 minutes for maintenance."

This command schedules a reboot in 10 minutes, sending the specified message to all users.

Examples

  1. Immediate Shutdown and Power-off:

    shutdown now
    

    This command immediately powers off the system.

  2. Schedule Reboot in 20 Minutes:

    shutdown -r +20 "System maintenance requires a reboot."
    

    Useful for system updates where a delay is needed to allow users to save their work.

  3. Cancel Pending Shutdown:

    shutdown -c
    

    This command cancels any scheduled shutdowns.

Common Issues

  • Permission Denied: The shutdown command typically requires root privileges. Run it with sudo if you are not the root user.
  • Incorrect Time Format: Make sure to use the correct time format (now, +m, or hh:mm). Otherwise, the system won’t interpret the command as expected.

Integration

The shutdown command can be used within scripts or combined with other commands. Here’s an example script to check disk usage and perform a reboot if usage is over 90%:

#!/bin/bash
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ $USAGE -gt 90 ]; then
   shutdown -r +5 "High disk usage detected. Rebooting in 5 minutes."
fi
  • reboot: A simpler alternative to shutdown for rebooting the system.
  • poweroff: Directly powers off the system without needing to specify additional options with shutdown.
  • halt: Stops all CPU functions, but leaves the system in a powered-on state.

For more detailed information and use cases, consult the official man pages by running man shutdown on your system. Additional online resources can be found on Linux distribution websites and forums.