Cmd.kill
func Cmd.kill(p: Cmd.Process, signal: string = "SIGTERM"): Result[void, Error]Cmd.kill sends the signal named, "SIGTERM", "SIGKILL", "SIGINT", "SIGHUP", "SIGQUIT", "SIGUSR1" or "SIGUSR2", to p's process group, and does not wait. It is Ok when p has exited already, and Err for a name the host does not have.
Details
- The signal goes to the command's process group: every stage of a pipeline, and the processes they started, get it.
Cmd.killreturns once the signal is sent. Cmd.wait gives the status. A command ended by signalnhas the status128 + n, as a shell reports it:143for SIGTERM,137for SIGKILL.- The name is the full name in capitals, such as
"SIGTERM". Any other name isErr. - On Windows the signal reaches each stage alone, and a command it ends has the status
1.
Basic examples
Send SIGTERM, the default, and wait for the command to end:
let p = try Cmd.start(Cmd.new("sleep", ["10"]))
try Cmd.kill(p)
println(try Cmd.wait(p))143Send SIGKILL instead, by name:
let p = try Cmd.start(Cmd.new("sleep", ["10"]))
try Cmd.kill(p, signal: "SIGKILL")
println(try Cmd.wait(p))137Scope
A command that has exited already is Ok, and nothing is sent:
let p = try Cmd.start(Cmd.new("true"))
try Cmd.wait(p)
println(Cmd.kill(p))Ok{ value: void }A pipeline gets the signal in every stage, so one call ends all of them:
let p = try Cmd.start(Cmd.pipe([Cmd.new("sleep", ["10"]), Cmd.new("cat")]))
try Cmd.kill(p)
println(try Cmd.wait(p))143A program that handles the signal ends its own way. This shell script saves its state on SIGTERM and exits with 0. It prints ready once its handler is set, so the signal does not arrive before it:
let out = try Os.openPipe()
defer { out.read.close() }
let script = "trap 'echo saving state; exit 0' TERM; echo ready; while :; do sleep 0.1; done"
let worker = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write)
try out.write.close()
println(try out.read.readLine())
try Cmd.kill(worker)
println(try out.read.readLine())
println(try Cmd.wait(worker))ready
saving state
0Named arguments
signal names the signal. Each one ends sleep, which handles none of them, with its own status:
for name in ["SIGTERM", "SIGKILL", "SIGINT", "SIGHUP", "SIGQUIT"] {
let p = try Cmd.start(Cmd.new("sleep", ["10"]))
try Cmd.kill(p, signal: name)
println("${name}: ${try Cmd.wait(p)}")
}SIGTERM: 143
SIGKILL: 137
SIGINT: 130
SIGHUP: 129
SIGQUIT: 131Applications
Ask a server to reload with SIGUSR1, which leaves it running:
let out = try Os.openPipe()
defer { out.read.close() }
let script = "trap 'echo reloading' USR1; trap 'exit 0' TERM; echo ready; while :; do sleep 0.1; done"
let server = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write)
try out.write.close()
defer { Cmd.stop(server) }
println(try out.read.readLine())
try Cmd.kill(server, signal: "SIGUSR1")
println(try out.read.readLine())
try Cmd.kill(server, signal: "SIGUSR1")
println(try out.read.readLine())ready
reloading
reloadingGive a command a deadline, then interrupt it as Ctrl-C would:
let job = try Cmd.start(Cmd.new("sleep", ["10"]))
let ctx = Context.new(timeout: 100ms)
if Cmd.wait(job, ctx: ctx) is Err {
println("no answer in time, interrupting")
try Cmd.kill(job, signal: "SIGINT")
}
println(try Cmd.wait(job))no answer in time, interrupting
130Properties and relations
The status is 128 plus the signal's number, and a second Cmd.kill after the exit is Ok:
let p = try Cmd.start(Cmd.new("sleep", ["10"]))
try Cmd.kill(p)
assert(try Cmd.wait(p) == 128 + 15)
assert(Cmd.kill(p) is Ok)
assert(Cmd.kill(p, signal: "SIGKILL") is Ok)
assert(try Cmd.wait(p) == 143)Possible issues
A short or lowercase name is Err, and nothing is sent:
let p = try Cmd.start(Cmd.new("sleep", ["10"]))
defer { Cmd.stop(p, grace: 0s) }
println(Cmd.kill(p, signal: "TERM"))
println(Cmd.kill(p, signal: "sigterm"))Err{ error: Error{ message: "unknown signal TERM" } }
Err{ error: Error{ message: "unknown signal sigterm" } }Without try, the Err from a misspelled name is dropped and the command keeps running. Write try Cmd.kill(...).
Cmd.kill does not wait, and a program may handle SIGTERM and keep running. This script ignores it, so a wait with a deadline runs out, and only SIGKILL ends it. Cmd.stop does both steps:
let out = try Os.openPipe()
defer { out.read.close() }
let script = "trap '' TERM; echo ready; while :; do sleep 0.1; done"
let stubborn = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write)
try out.write.close()
println(try out.read.readLine())
try Cmd.kill(stubborn)
let ctx = Context.new(timeout: 300ms)
println(Cmd.wait(stubborn, ctx: ctx))
try Cmd.kill(stubborn, signal: "SIGKILL")
println(try Cmd.wait(stubborn))ready
Err{ error: Error{ message: "timeout" } }
137See also
Cmd.stop · Cmd.wait · Cmd.start · Cmd.pid · Cmd.Process · Context.new
