halt Command: Tutorial & Examples

Shut Down Or Halt The Linux System From The Command Line

The halt command is a fundamental utility used to stop all CPU functions and safely bring a Linux system to a halt from the command line. Typically requiring root or superuser privileges, it instructs the kernel to terminate all processes and unmount file systems, effectively stopping system activity. Depending on system configuration and options, halt may power off the machine or simply stop the CPU. It is an essential tool for system administrators managing servers and virtual machines without graphical interfaces.

What halt Does

The primary purpose of the halt command is to stop the CPU and all system activity, effectively bringing the system to a complete stop. Unlike shutdown, which gracefully stops services and notifies logged-in users before halting, halt immediately signals the kernel to stop the system. On some systems, invoking halt with specific options also powers off the machine, but in others, it only stops the CPU without turning off hardware. The exact behavior depends on the Linux distribution, kernel support, and init system.

How halt Works

When executed, halt sends a signal to the Linux kernel to terminate all running processes, unmount file systems safely, and stop CPU operations. Traditionally, this was done by invoking system calls like reboot(LINUX_REBOOT_CMD_HALT) to halt the system without powering off. Modern Linux distributions often use systemd, where halt is a symbolic link to systemctl halt or systemctl poweroff.

The command's effect depends on the underlying init system:

  • SysVinit and similar: halt calls scripts in /etc/init.d/ or /etc/rc.d/ to stop services and unmount filesystems before halting the CPU.
  • systemd: halt typically maps to a systemctl command that manages system states, ensuring orderly shutdown and hardware power off if requested.

If the hardware supports it and the -p option is used, the machine powers off after halting.

Common Command Line Parameters

The halt command supports several options to control its behavior:

  1. -f
    Force immediate halt without calling shutdown scripts.
    This skips unmounting filesystems and can cause file-system-corruption.

  2. -p
    Power off the machine after halting if hardware and kernel support it.

  3. -w
    Do not halt the system; only write a shutdown record to the /var/log/wtmp file.

  4. -h
    Halt the system (default behavior). Sometimes implicit.

  5. --help
    Display a help message showing available options.

Practical Examples Using halt

Here are practical examples demonstrating common halt usage scenarios:

  1. Halt the system safely (requires root privileges):

    sudo halt
    

    Sample output: Usually no output is shown. The system begins the halt process.

  2. Halt and power off the machine:

    sudo halt -p
    

    This instructs the system to power off the machine after halting.

  3. Force immediate halt without running shutdown scripts (use with caution):

    sudo halt -f
    

    This can be used in emergencies but risks disk-error and data loss.

  4. Write shutdown record without halting:

    sudo halt -w
    

    Updates the shutdown logs but leaves the system running.

  5. Check if halt is a symbolic link to systemctl (on systemd systems):

    ls -l $(which halt)
    

    Sample output:

    lrwxrwxrwx 1 root root 9 Apr 12 10:00 /usr/bin/halt -> systemctl
    
  6. Schedule a system halt in 10 minutes using at:

    echo "sudo halt" | at now + 10 minutes
    
  7. Using halt in a script to safely stop the system:

    #!/bin/bash
    echo "System will halt in 1 minute. Please save your work."
    sleep 60
    sudo halt -p
    

Cheatsheet

Quick reference for common halt options:

  • halt : Halt the system (default).
  • halt -p : Halt and power off.
  • halt -f : Force immediate halt (dangerous).
  • halt -w : Write shutdown record only.
  • halt --help : Show help message.

Potential Problems and Troubleshooting

  • Using halt -f without proper preparation can cause file-system-corruption or disk-error because filesystems might not be unmounted cleanly.

  • If the machine does not power off after halt, try halt -p or use poweroff.

  • On systemd systems, halt may be a symlink to systemctl. Confirm with:

    ls -l $(which halt)
    
  • Permission denied errors indicate you are not running as root or via sudo.

  • Remote use of halt via SSH should be done cautiously to avoid disrupting other users or network-failure.

  • If the system fails to halt properly, check logs in /var/log/syslog or via journalctl for errors.

  • After improper halts, running a file system check with fsck may be necessary.

Comparison With Alternatives

  • shutdown: Provides a graceful shutdown by notifying users and stopping services before halting or rebooting. Example: shutdown -h now.
  • poweroff: Powers off the system immediately, similar to halt -p.
  • reboot: Halts and then restarts the system.
  • On modern systems using systemd, these commands are often symbolic links or wrappers around systemctl commands managing system power states.

Use shutdown for planned maintenance, halt for stopping CPU without rebooting, and poweroff to turn off hardware.

Tips and Best Practices

  • Prefer shutdown for planned halts to avoid data loss.
  • Use halt -f only in emergencies.
  • Check your distribution’s behavior for halt because implementations vary.
  • Notify logged-in users before halting remote servers.
  • Automate halts safely with cron or at.
  • Always verify system logs after halting to detect issues.
  • Test halt commands on non-critical systems first.

Security Considerations

Only trusted users should be allowed to execute halt, as improper use can disrupt services and compromise system availability. Control access through sudo configuration. For example, restrict halt permission by adding to /etc/sudoers:

    youruser ALL=(ALL) NOPASSWD: /sbin/halt

Avoid exposing halt commands via network services or scripts accessible by untrusted users.

Scripting And Automation With halt

The halt command can be used in scripts to automate system shutdowns. For example, to halt a system at a scheduled time, combine halt with cron or at:

  • Add to crontab (crontab -e) to halt at 11 PM daily:

    0 23 * * * /sbin/halt -p
    
  • Use in maintenance scripts with appropriate warning messages and logging.

Always ensure scripts run with sufficient privileges and notify users when possible.

Advanced Usage And Lesser-Known Features

  • Some systems support additional options like --no-wall to suppress broadcast messages.
  • Embedded systems or rescue environments may use halt without powering off to preserve hardware state.
  • Combining halt with kernel parameters can influence behavior during startup/shutdown.
  • halt can be used in recovery scenarios when normal shutdown fails.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA