Cmd.stop
func Cmd.stop(p: Cmd.Process, grace: Time.Duration = 5s): Result[int, Error]Cmd.stop ends p: SIGTERM, then SIGKILL when it is still running after grace, or SIGKILL alone when grace is zero or negative, and returns its status as Cmd.wait does. It panics when grace has years, months or weeks.
Details
Cmd.stopsignals the whole process group thatCmd.startmade, so every stage of a pipeline gets the signal.- Without
grace:, it waits5safter SIGTERM before SIGKILL. - It waits for the command to exit and returns its status. A command ended by a signal has
128 + n:143for SIGTERM,137for SIGKILL. That status isOk, notErr. - On a command that has exited already it sends nothing and returns its status, as
Cmd.waitdoes. Calling it twice is safe. - A command still running when the script ends is left running. Stop it in a deferred block.
- On Windows a command that
Cmd.stopends has status1.
Basic examples
Cmd.stop(p) sends SIGTERM and returns the status. sleep dies of it, 128 + 15:
let p = try Cmd.start(Cmd.new("sleep", ["30"]))
println(try Cmd.stop(p))143On a command that has exited, Cmd.stop sends nothing and returns the status it exited with:
let p = try Cmd.start(Cmd.new("sh", ["-c", "exit 3"]))
println(try p.wait())
println(try Cmd.stop(p))3
3Scope
Call it with a dot on the process:
let p = try Cmd.start(Cmd.new("sleep", ["30"]))
println(try p.stop())143A pipeline's stages share one process group, so one call stops them all. The status is that of the rightmost stage that failed:
let p = try Cmd.start(Cmd.pipe([Cmd.new("sleep", ["30"]), Cmd.new("cat")]))
println(try p.stop())143A command that handles SIGTERM can finish its work and exit with its own status. This shell script traps the signal, prints a line, and exits with 0. The program reads ready first, so the trap is in place before the signal arrives:
let out = try Os.openPipe()
defer { out.read.close() }
let script = "trap 'echo saving; exit 0' TERM; echo ready; while true; do sleep 1; done"
let server = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write)
try out.write.close()
println(try out.read.readLine())
println(try server.stop())
println(try out.read.readLine())ready
0
savingNamed arguments
grace: is how long Cmd.stop waits after SIGTERM before it sends SIGKILL. This script ignores SIGTERM, so it runs until the grace ends and SIGKILL ends it, 128 + 9:
let out = try Os.openPipe()
defer { out.read.close() }
let script = "trap '' TERM; echo ready; while true; do sleep 1; done"
let p = try Cmd.start(Cmd.new("sh", ["-c", script]), stdout: out.write)
try out.write.close()
println(try out.read.readLine())
println(try p.stop(grace: 200ms))ready
137A grace of zero, or a negative one, sends SIGKILL alone, with no SIGTERM before it:
let p = try Cmd.start(Cmd.new("sleep", ["30"]))
println(try p.stop(grace: 0s))137Applications
Stop a helper process when the script ends, however it ends. A deferred stop runs on return, on Err, and on a panic:
let server = try Cmd.start(Cmd.new("sleep", ["30"]))
defer { server.stop(grace: 1s) }
println("server ${server.pid() > 0}")server trueRun a command with a time limit. A context on Cmd.wait ends only the wait, and the command goes on running, so stop it once the wait fails:
func runFor(c: Cmd, limit: Time.Duration): Result[int, Error] {
let p = try Cmd.start(c)
let ctx = Context.new(timeout: limit)
defer { ctx.cancel() }
let status = p.wait(ctx: ctx)
if status.isOk() { return status }
println("timed out: ${c.string()}")
p.stop(grace: 1s)
}
println(try runFor(Cmd.new("sh", ["-c", "exit 4"]), 5s))
println(try runFor(Cmd.new("sleep", ["30"]), 100ms))4
timed out: sleep 30
143Properties and relations
Cmd.stop returns what Cmd.wait returns afterward, and a second stop sends nothing and returns the same status:
let p = try Cmd.start(Cmd.new("sleep", ["30"]))
let status = try p.stop()
assert(try p.wait() == status)
assert(try p.stop() == status)
println(status)143Possible issues
A command that SIGTERM ends has a nonzero status. Cmd.stop returns it as Ok, so check the status when it matters:
let p = try Cmd.start(Cmd.new("sleep", ["30"]))
let status = try p.stop()
println(if status == 0 { "clean exit" } else { "status ${status}" })status 143A grace with years, months or weeks has no fixed length, so Cmd.stop panics:
let p = try Cmd.start(Cmd.new("sleep", ["1"]))
let status = p.stop(grace: Time.duration(weeks: 1))
// panic: Cmd.stop needs a duration without years, months or weeks, which have no fixed length
println(status)See also
Cmd.kill, Cmd.wait, Cmd.start, Cmd.pid, Cmd.Process, Context.new, Time.duration
