Skip to content

Class Process ​

Starts programs and signals them.

lua
Process.spawn("sh", { "-c", "date +%H:%M" }, {
	on_stdout = function(line) print("the time is", line) end,
	on_exit = function(report) print("date exited with", report.status) end,
})

Functions ​

Process.spawn(program: string, args: string[]?, options: ProcessOptions?) -> integerspawn ​

Starts program without waiting for it to exit.

  • A name without a slash is looked up in PATH.
  • There is no shell. For a command line, start sh with { "-c", line }.
  • The process gets Hazel's environment, WAYLAND_DISPLAY included, plus options.env.
  • Its standard input is empty. Its output is thrown away unless on_stdout or on_stderr is set.
  • It gets a process group of its own, so Process.kill and timeout reach the programs it starts too.
  • At a reload, a process with any on_* callback gets SIGTERM unless survives_reload is set. One started without callbacks keeps running.
  • When Hazel quits, every process it started gets SIGTERM, then SIGKILL half a second later if it's still running.

Errors when the program can't be started, for example when it doesn't exist.

lua
if not wm.is_reload then
	Process.spawn("sh", { "-c", "echo session started >> /tmp/hazel-session.log" })
end

Process.kill(pid: integer, signal: "term" | "kill" | "int" | "hup"?) -> booleankill ​

Sends signal to the process group of a process started with Process.spawn.

  • signal defaults to "term".
  • true when the process was still running. false when it has exited, or Hazel didn't start it.

Errors for any other signal name.

lua
local sleeper = Process.spawn("sleep", { "60" })
Timer.after(1000, function() Process.kill(sleeper, "int") end)

Options ​

options.env: table<string, string>?env ​

Variables added to the environment the process gets. They replace ones with the same name.

options.cwd: string?cwd ​

The directory the process starts in.

  • nil: Hazel's own working directory.

options.timeout: number?timeout ​

Milliseconds the process may run. Then its process group gets SIGTERM, and SIGKILL a second later if it's still running.

  • The exit report has timed_out set.
  • A negative timeout is ignored.

Errors for an infinite or far too large timeout.

lua
Process.spawn("sleep", { "30" }, {
	timeout = 2000,
	on_exit = function(report) print("timed out:", report.timed_out) end,
})

options.on_stdout: fun(line: string)?on_stdout ​

Called for each line the process writes to standard output, without the line ending (\n or \r\n).

  • Each call has its own instruction budget. An error in it is logged.
  • A last line with no line ending arrives when the process exits.

options.on_stderr: fun(line: string)?on_stderr ​

The same for standard error.

options.on_exit: fun(report: ExitReport)?on_exit ​

Called once when the process has exited, after its last output lines.

options.survives_reload: boolean?survives_reload ​

Keeps a process with callbacks running through a reload.

  • Its callbacks are dropped either way, with the config that set them.

Exit report ​

report.status: integer?status ​

The exit code.

  • nil when a signal killed the process.

report.signal: integer?signal ​

The number of the signal that killed the process, such as 15 for SIGTERM.

  • nil when it exited by itself.

report.timed_out: booleantimed_out ​

Whether timeout ran out and Hazel signalled the process.