LinuxCommandLibrary

sv

Control and monitor services managed by runit

TLDR

Start a service

$ sudo sv up [path/to/service]
copy

Stop a service
$ sudo sv down [path/to/service]
copy

Get service status
$ sudo sv status [path/to/service]
copy

Reload a service
$ sudo sv reload [path/to/service]
copy

Start a service, but only if it's not running and don't restart it if it stops
$ sudo sv once [path/to/service]
copy

SYNOPSIS

sv {command} {service_directory}

PARAMETERS

start
    Starts the service in the specified service directory.

stop
    Stops the service in the specified service directory, sending a TERM signal and then a KILL signal if necessary.

restart
    Restarts the service in the specified service directory. It stops and then starts the service.

pause
    Pauses the service in the specified service directory by sending a STOP signal.

continue
    Continues a paused service in the specified service directory by sending a CONT signal.

once
    Starts the service if it's not running, but does not restart it if it stops. Useful for running services once.

shutdown
    Sends a shutdown signal to the service.

status
    Displays the current status of the service in the specified service directory. This will include current state, uptime, and PID.

force-stop
    Immediately stops the service by sending a KILL signal.

force-restart
    Immediately restarts the service by sending a KILL signal to stop, then starting the service.

force-reload
    Sends HUP signal to the service process to trigger a reload of configuration files (if the service supports it).

reload
    Tries to reload the service using its reload command. If no reload command is defined then sv sends HUP signal to the service process to trigger a reload of configuration files.

check
    Checks if the service is running and restarts it if it's not.

log
    Toggles logging for the service.

clear
    Clears the log directory for the service, useful for managing log file sizes.

signal signal_name
    Sends an arbitrary signal to the service. Example usage: sv signal USR1 /service/name

{service_directory}
    The directory containing the service's configuration files, including the run script.

DESCRIPTION

The sv command is a powerful tool used to control and monitor services managed by the daemontools suite. It allows you to start, stop, restart, pause, continue, and check the status of these services. Daemontools is a collection of tools for managing and supervising long-running processes. sv acts as the interface between the user and the svc program, which directly interacts with the supervised process. It utilizes signal sending to control the service, allowing for graceful shutdown and restarts. It's designed to be robust and reliable, ensuring services are always running as intended. By using sv, administrators can easily maintain the availability and stability of their applications running under daemontools.

CAVEATS

The sv command relies on the proper setup of daemontools for each service. Ensure that the run script in the service directory is executable and handles signals correctly for graceful shutdown and restarts. It's crucial to manage service dependencies correctly within the daemontools configuration.

sv commands must be run with sufficient privileges (usually root) to control services. Without appropriate permissions, operations might fail.

SERVICE DIRECTORY STRUCTURE

Each service managed by daemontools requires a dedicated directory. This directory typically contains a run script, which defines how the service is started. It may also include other files such as log (directory for logs) and down (file which if present, prevents the service from starting).

Example: A typical service directory structure might look like this:
/service/my_app/
    run
    log/
        run

RUN SCRIPT

The run script is the heart of a daemontools service. It's an executable script that starts the actual service process. This script should be designed to handle signals gracefully, such as TERM for shutdown or HUP for reload. It is responsible for setting up the environment, starting the service, and ensuring the service stays running. Ideally, the run script will automatically restart the service if it terminates unexpectedly.

SIGNAL HANDLING

Daemontools and sv rely heavily on signal handling. When you issue a command like 'stop' or 'restart', sv sends signals (e.g., TERM, KILL) to the service process. The run script should be written to respond to these signals appropriately, allowing for a clean shutdown or a graceful restart. Proper signal handling is critical for maintaining service stability and preventing data loss.

HISTORY

sv is an integral part of the daemontools package, created by Daniel J. Bernstein. It was designed to provide a reliable and straightforward mechanism for managing long-running processes, addressing the limitations of traditional init systems. Daemontools and sv gained popularity due to their simplicity, robustness, and focus on ensuring high service availability.

SEE ALSO

svc(8), daemontools(7)

Copied to clipboard