diff --git a/server/README.md b/server/README.md index 4d9eb2cc..6bcf92b4 100644 --- a/server/README.md +++ b/server/README.md @@ -220,6 +220,25 @@ brew install oxigraph It installs the `oxigraph_server` binary. [See the usage documentation to know how to use it](#usage). +## Systemd + +It is possible to run Oxigraph in the background using systemd. + +For that, you can use the following `oxigraph_server.service` file (it might be inserted into `/etc/systemd/system/` or `$HOME/.config/systemd/user`): +```ini +[Unit] +Description=Oxigraph database server +After=network-online.target +Wants=network-online.target + +[Service] +Type=notify +ExecStart=/PATH/TO/oxigraph_server serve --location /PATH/TO/OXIGRAPH/DATA + +[Install] +WantedBy=multi-user.target +``` + ## Migration guide ### From 0.2 to 0.3 diff --git a/server/src/main.rs b/server/src/main.rs index 1264f132..57257e1b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -17,9 +17,13 @@ use sparesults::{QueryResultsFormat, QueryResultsSerializer}; use std::borrow::Cow; use std::cell::RefCell; use std::cmp::{max, min}; +#[cfg(target_os = "linux")] +use std::env; use std::ffi::OsStr; use std::fs::File; use std::io::{self, stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +#[cfg(target_os = "linux")] +use std::os::unix::net::UnixDatagram; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::str::FromStr; @@ -776,6 +780,8 @@ fn serve(store: Store, bind: String, read_only: bool, cors: bool) -> anyhow::Res }; server.set_global_timeout(HTTP_TIMEOUT); server.set_server_name(concat!("Oxigraph/", env!("CARGO_PKG_VERSION")))?; + #[cfg(target_os = "linux")] + systemd_notify_ready()?; eprintln!("Listening for requests at http://{}", &bind); server.listen(bind)?; Ok(()) @@ -1698,6 +1704,14 @@ impl Write for ReadForWriteWriter { } } +#[cfg(target_os = "linux")] +fn systemd_notify_ready() -> io::Result<()> { + if let Some(path) = env::var_os("NOTIFY_SOCKET") { + UnixDatagram::unbound()?.send_to(b"READY=1", path)?; + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*;