how start processes script in way allows me terminate them?
basically, can terminate main script, terminating external processes main script starts has been issue. googled crazy perl 6 solutions. post question , thought i'd open question solutions in other languages.
starting external processes easy perl 6:
my $proc = shell("possibly_long_running_command"); shell returns process object after process finishes. so, don't know how programmatically find out pid of running process because variable $proc isn't created until external process finishes. (side note: after finishes, $proc.pid returns undefined any, doesn't tell me pid used have.)
here code demonstrating of attempts create "self destructing" script:
#!/bin/env perl6 "pid of main script: $*pid"; # limit run time of script promise.in(10).then( { "took long! killing job pid of $*pid"; shell "kill $*pid" } ); $example = shell('echo "pid of bash command: $$"; sleep 20; echo "pid of bash command after sleeping still $$"'); "this line never printed"; this results in following output kills main script, not externally created process (see output after word terminated):
[prompt]$ ./self_destruct.pl6 pid of main script: 30432 pid of bash command: 30436 took long! killing job pid of 30432 terminated [prompt]$ pid after sleeping still 30436 by way, pid of sleep different (i.e. 30437) according top.
i'm not sure how make work proc::async. unlike result of shell, asynchronous process object creates doesn't have pid method.
i looking perl 6 solution, i'm open solutions in python, perl 5, java, or language interacts "shell" reasonably well.
for perl 6, there seems proc::async module
proc::async allows run external commands asynchronously, capturing standard output , error handles, , optionally write standard input.
# command arguments $proc = proc::async.new('echo', 'foo', 'bar'); # subscribe new output out , err handles: $proc.stdout.tap(-> $v { print "output: $v" }); $proc.stderr.tap(-> $v { print "error: $v" }); "starting..."; $promise = $proc.start; # wait external program terminate await $promise; "done."; method kill:
kill(proc::async:d: $signal = "hup") sends signal running program. signal can signal name ("kill" or "sigkill"), integer (9) or element of signal enum (signal::sigkill).
an example on how use it:
#!/usr/bin/env perl6 use v6; 'start'; $proc = proc::async.new('sleep', 10); $promise= $proc.start; 'process started'; sleep 2; $proc.kill; await $promise; 'process killed'; as can see, $proc has method kill process.
Comments
Post a Comment