% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/osrm_stop_server.R
\name{osrm_stop}
\alias{osrm_stop}
\title{Stop an OSRM Server}
\usage{
osrm_stop(
  server = NULL,
  id = NULL,
  port = NULL,
  pid = NULL,
  wait = 1000L,
  quiet = FALSE
)
}
\arguments{
\item{server}{Optional OSRM job process (an \code{osrm_server} object
inheriting from \code{processx::process}) returned by \code{osrm_start_server()}.}

\item{id}{Optional character id from \code{osrm_servers()}.}

\item{port}{Optional integer TCP port.}

\item{pid}{Optional integer process id.}

\item{wait}{Integer milliseconds to wait for clean shutdown (default \code{1000}).}

\item{quiet}{Logical; suppress messages (default \code{FALSE}).}
}
\value{
A list with fields \code{id}, \code{pid}, \code{port}, \code{stopped} (logical).
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#stable}{\figure{lifecycle-stable.svg}{options: alt='[Stable]'}}}{\strong{[Stable]}}

Terminates an \code{osrm-routed} process launched by \code{osrm_start()} or
\code{osrm_start_server()}. Can also stop external servers by PID or ID.
}
\details{
This function provides a flexible way to stop a running OSRM process. If no
arguments are specified, it defaults to stopping the most recently started
server that is still alive in the current session.

You can also stop a specific server by providing:
\itemize{
\item The OSRM job process (a \code{processx::process} object) returned by
\code{osrm_start()} or \code{osrm_start_server()}.
\item The server's \code{id}, \code{port}, or \code{pid} (use \code{osrm_servers()} to find these).
}

\strong{Advanced Use:}
You can stop an external \code{osrm-routed} process (one not started by the current
R session) by passing its PID, or by finding it via \code{osrm_servers(include_all = TRUE)}
and passing its \code{id} or \code{port}. This requires permission to signal the process.
}
\examples{
\donttest{
if (identical(Sys.getenv("OSRM_EXAMPLES"), "true")) {
  install_dir <- osrm_install(
    version = "latest",
    path_action = "session",
    quiet = TRUE
  )

  # copy example OSM PBF into a temporary workspace to avoid polluting pkg data
  pbf_path <- system.file("extdata/cur.osm.pbf", package = "osrm.backend")
  osrm_dir <- file.path(tempdir(), paste0("osrm-", Sys.getpid()))
  dir.create(osrm_dir, recursive = TRUE)
  tmp_pbf <- file.path(osrm_dir, "cur.osm.pbf")
  file.copy(from = pbf_path, to = tmp_pbf, overwrite = TRUE)
  graph <- osrm_prepare_graph(tmp_pbf, overwrite = TRUE, threads = 1L)

  srv <- osrm_start_server(graph$osrm_job_artifact, port = 6000, threads = 1L)

  # Stop by passing the process object
  osrm_stop(srv)

  # Or stop by port after the process is registered
  osrm_stop(port = 6000)

  osrm_uninstall(
    dest_dir = install_dir,
    clear_path = TRUE,
    force = TRUE,
    quiet = TRUE
  )
  unlink(osrm_dir, recursive = TRUE)
}

\dontrun{
  # Advanced: Stop an external server by PID
  # 1. Find the PID of an external server
  srvs <- osrm_servers(include_all = TRUE)
  # 2. Stop it by PID
  if (nrow(srvs) > 0) {
    osrm_stop(pid = srvs$pid[1])
  }
  
  # Or stop by its external ID (e.g., "sys-12345")
  osrm_stop(id = "sys-12345")
}
}
}
\seealso{
\code{\link[=osrm_start]{osrm_start()}}, \code{\link[=osrm_servers]{osrm_servers()}}, \code{\link[=osrm_stop_all]{osrm_stop_all()}}
}
