OpenHAP::Daemon(3p) Perl Library Manual OpenHAP::Daemon(3p)

OpenHAP::Daemon - Daemonization and process management

    use OpenHAP::Daemon;
    
    # Daemonize the process
    OpenHAP::Daemon->daemonize('/var/log/myapp.log');
    
    # Write PID file
    OpenHAP::Daemon->write_pidfile('/var/run/myapp.pid');
    
    # Check if daemon is running
    my $pid = OpenHAP::Daemon->check_running('/var/run/myapp.pid');
    if ($pid) {
        print "Daemon running with PID $pid\n";
    }

OpenHAP::Daemon provides utilities for daemonizing processes and managing PID files on OpenBSD. It handles forking, session creation, and file descriptor redirection following OpenBSD conventions.

    $class->daemonize($logfile)

Fork into background, detach from terminal, and redirect standard file descriptors. The parent process exits successfully, and only the child continues. Standard input is redirected to /dev/null, stdout and stderr are redirected to the specified log file.

Dies if forking or session creation fails.

    $class->write_pidfile($path)

Write the current process ID to the specified file. Returns true on success, undef on failure. Errors are logged.

    my $pid = $class->read_pidfile($path)

Read a PID from the specified file. Returns the PID as an integer, or undef if the file doesn't exist, cannot be read, or doesn't contain a valid PID.

    my $pid = $class->check_running($pidfile)

Check if a daemon is running based on its PID file. Returns the PID if the process exists, undef otherwise. This method reads the PID file and verifies the process is still running using kill(0).

The daemonize() method sets "$DB::inhibit_exit = 0" after forking to ensure proper cleanup when running under the debugger.

PID files should be stored in /var/run/ with appropriate permissions.

OpenHAP::Log, daemon(3), rc.d(8)

Dick Olsson <hi@senzilla.io>

2026-07-27 OpenBSD