我需要某种特殊的设置来控制LED墙。可悲的是,我不能真正改变我使用的编程语言。我的设置如下所示:
引导过程之后,开始处理(一些疯狂的Java fork ...)。处理草图会扫描文件夹中的子文件夹(可以启动其他草图并控制LED墙)并启动Web服务器。服务器将显示所有扫描文件夹的列表。在单击上,“ Web服务器”将通过ProcessBuilder启动选定的草图。处理草图如下所示:
import http.*;
import java.util.*;
import java.lang.*;
SimpleHTTPServer server;
String prog = "";
int ExitValue = 1;
ProcessBuilder preparedsketch;
Process runningsketch;
void setup() {
SimpleHTTPServer.useIndexHtml = false;
server = new SimpleHTTPServer(this);
TemplateFileHandler templateHandler = new ResultFiles("index.ftl");
server.createContext("", templateHandler);
}
class ResultFiles extends TemplateFileHandler {
public ResultFiles(String templateFileName) {
super(templateFileName);
}
void createMap() {
Map<String, String> params = queryToMap();
if (params.containsKey("prog")) {
if (params.get("prog").equals(prog)) {
println("Has not changed");
} else {
println("PrevProcess: " + runningsketch);
if (runningsketch != null) {
println("Killing: " + runningsketch);
runningsketch.destroy();
}
prog = params.get("prog");
try {
runningsketch = new ProcessBuilder("/Users/kessleml/dev/pixelpusher/base/processing-quit.sh", "--sketch=/Users/kessleml/dev/pixelpusher/base/sketches/pixelpusher_colourcycle_halloween", "--run").start();
// runningsketch = new ProcessBuilder("/usr/local/bin/processing-java", "--force", "--sketch=" + sketchPath("sketches/" + prog + "/"), "--no-java", "--run").start();
} catch (IOException ex) {
println(ex);
}
println("ProjChagned: " + prog);
println("NewProcess: " + runningsketch);
}
}
File files = new File(sketchPath("sketches"));
String[] fileslist = files.list();
addVariable("files", fileslist);
addVariable("selectedprog", prog);
}
}
到现在为止一切正常。但是,如果我进行更改(当然,请单击网站上的另一个草图),我当然想关闭一个正在运行的(和循环的)草图。问题是:
当我通过runninngsketch = new ProcessBuilder("Path/To/ProcessingCLI", "--sketch=Path/To/Selected/Sketch", "--run").start();
多个过程启动选定的草图时。造成这种情况的原因是ProcessingCLI文件:
#!/bin/sh
# Prevents processing-java from stealing focus, see:
# https://github.com/processing/processing/issues/3996.
OPTION_FOR_HEADLESS_RUN=""
for ARG in "$@"
do
if [ "$ARG" = "--build" ]; then
OPTION_FOR_HEADLESS_RUN="-Djava.awt.headless=true"
fi
done
cd "/Applications/Processing.app/Contents/Java" && /Applications/Processing.app/Contents/PlugIns/jdk1.8.0_74.jdk/Contents/Home/jre/bin/java -Djna.nosys=true $OPTION_FOR_HEADLESS_RUN -cp "ant-launcher.jar:ant.jar:core.jar:jna.jar:pde.jar:core/library/core.jar:core/library/gluegen-rt-natives-linux-amd64.jar:core/library/gluegen-rt-natives-linux-armv6hf.jar:core/library/gluegen-rt-natives-linux-i586.jar:core/library/gluegen-rt-natives-macosx-universal.jar:core/library/gluegen-rt-natives-windows-amd64.jar:core/library/gluegen-rt-natives-windows-i586.jar:core/library/gluegen-rt.jar:core/library/jogl-all-natives-linux-amd64.jar:core/library/jogl-all-natives-linux-armv6hf.jar:core/library/jogl-all-natives-linux-i586.jar:core/library/jogl-all-natives-macosx-universal.jar:core/library/jogl-all-natives-windows-amd64.jar:core/library/jogl-all-natives-windows-i586.jar:core/library/jogl-all.jar:modes/java/mode/antlr.jar:modes/java/mode/classpath-explorer-1.0.jar:modes/java/mode/com.ibm.icu.jar:modes/java/mode/JavaMode.jar:modes/java/mode/jdi.jar:modes/java/mode/jdimodel.jar:modes/java/mode/jdtCompilerAdapter.jar:modes/java/mode/jsoup-1.7.1.jar:modes/java/mode/org.eclipse.core.contenttype.jar:modes/java/mode/org.eclipse.core.jobs.jar:modes/java/mode/org.eclipse.core.resources.jar:modes/java/mode/org.eclipse.core.runtime.jar:modes/java/mode/org.eclipse.equinox.common.jar:modes/java/mode/org.eclipse.equinox.preferences.jar:modes/java/mode/org.eclipse.jdt.core.jar:modes/java/mode/org.eclipse.osgi.jar:modes/java/mode/org.eclipse.text.jar:modes/java/mode/org.netbeans.swing.outline.jar" processing.mode.java.Commander "$@"
因此,ProcessBuilder启动了三个进程:一个sh进程,它启动两个Java-Children-Processs。当我使用runningsketch.destroy()
它只会杀死sh进程。这两个Java进程继续运行。(由于我是在MacOS Yosemite上进行开发的,因此不知道这是否也是由于以下错误引起的:http : //bugs.java.com/bugdatabase/view_bug.do? bug_id=4770092。最终产品应在Linux机器上运行。)
我的解决方案是编写一个新的sh脚本,该脚本通过以下方式杀死所有子进程trap
:
#!/bin/sh
OPTION_FOR_HEADLESS_RUN=""
function killAllChildren {
kill -9 -$(ps -o pgid= $$ | grep -o '[0-9]*')
}
trap killAllChildren SIGTERM SIGKILL
# trap "trap - SIGTERM && kill -- $$" SIGINT SIGTERM EXIT
cd "/Applications/Processing.app/Contents/Java"
/Applications/Processing.app/Contents/PlugIns/jdk1.8.0_74.jdk/Contents/Home/jre/bin/java -Djna.nosys=true $OPTION_FOR_HEADLESS_RUN -cp "ant-launcher.jar:ant.jar:core.jar:jna.jar:pde.jar:core/library/core.jar:core/library/gluegen-rt-natives-linux-amd64.jar:core/library/gluegen-rt-natives-linux-armv6hf.jar:core/library/gluegen-rt-natives-linux-i586.jar:core/library/gluegen-rt-natives-macosx-universal.jar:core/library/gluegen-rt-natives-windows-amd64.jar:core/library/gluegen-rt-natives-windows-i586.jar:core/library/gluegen-rt.jar:core/library/jogl-all-natives-linux-amd64.jar:core/library/jogl-all-natives-linux-armv6hf.jar:core/library/jogl-all-natives-linux-i586.jar:core/library/jogl-all-natives-macosx-universal.jar:core/library/jogl-all-natives-windows-amd64.jar:core/library/jogl-all-natives-windows-i586.jar:core/library/jogl-all.jar:modes/java/mode/antlr.jar:modes/java/mode/classpath-explorer-1.0.jar:modes/java/mode/com.ibm.icu.jar:modes/java/mode/JavaMode.jar:modes/java/mode/jdi.jar:modes/java/mode/jdimodel.jar:modes/java/mode/jdtCompilerAdapter.jar:modes/java/mode/jsoup-1.7.1.jar:modes/java/mode/org.eclipse.core.contenttype.jar:modes/java/mode/org.eclipse.core.jobs.jar:modes/java/mode/org.eclipse.core.resources.jar:modes/java/mode/org.eclipse.core.runtime.jar:modes/java/mode/org.eclipse.equinox.common.jar:modes/java/mode/org.eclipse.equinox.preferences.jar:modes/java/mode/org.eclipse.jdt.core.jar:modes/java/mode/org.eclipse.osgi.jar:modes/java/mode/org.eclipse.text.jar:modes/java/mode/org.netbeans.swing.outline.jar" processing.mode.java.Commander "$@"
但是不知何故,这也不起作用。即使启动了新的sh脚本并向启动的sh进程发送了例如SIGTERM,也不会破坏两个Java进程,也不会破坏sh进程。
我找到了解决方案:
Java发送SIGTERM信号,因此我不得不捕获该信号。但是java / processing文件不是问题,sh脚本没有按预期工作。
我必须将& wait
和添加到脚本中。否则SIGTERM不能被困住(请参阅这篇文章:https : //apple.stackexchange.com/questions/123631/why-does-a-shell-script-trapping-sigterm-work-when-run-manually-but-not- when-ru)。
同样,杀人过程也没有成功。我必须杀死所有孩子,但sh脚本本身却不是sh脚本的父进程(在此用例中是Web服务器等)。因此,我编写了一个函数来查找所有子进程并杀死它们。像这样的事情kill -9 -$(ps -o pgid= $$ | grep -o '[0-9]*')
没有奏效,因为它们杀死了整棵树。最后,sh文件如下所示:
#!/bin/sh
function killAllChildren {
getChild $$
pkill -TERM -P $$
}
function getChild() {
cpids=`pgrep -P $1|xargs`
for cpid in $cpids;
do
kill -15 $cpid
getChild $cpid
done
}
trap killAllChildren SIGUSR1 SIGTERM SIGKILL EXIT
cd "/Applications/Processing.app/Contents/Java"
/Applications/Processing.app/Contents/PlugIns/jdk1.8.0_74.jdk/Contents/Home/jre/bin/java -Djna.nosys=true -cp "ant-launcher.jar:ant.jar:core.jar:jna.jar:pde.jar:core/library/core.jar:core/library/gluegen-rt-natives-linux-amd64.jar:core/library/gluegen-rt-natives-linux-armv6hf.jar:core/library/gluegen-rt-natives-linux-i586.jar:core/library/gluegen-rt-natives-macosx-universal.jar:core/library/gluegen-rt-natives-windows-amd64.jar:core/library/gluegen-rt-natives-windows-i586.jar:core/library/gluegen-rt.jar:core/library/jogl-all-natives-linux-amd64.jar:core/library/jogl-all-natives-linux-armv6hf.jar:core/library/jogl-all-natives-linux-i586.jar:core/library/jogl-all-natives-macosx-universal.jar:core/library/jogl-all-natives-windows-amd64.jar:core/library/jogl-all-natives-windows-i586.jar:core/library/jogl-all.jar:modes/java/mode/antlr.jar:modes/java/mode/classpath-explorer-1.0.jar:modes/java/mode/com.ibm.icu.jar:modes/java/mode/JavaMode.jar:modes/java/mode/jdi.jar:modes/java/mode/jdimodel.jar:modes/java/mode/jdtCompilerAdapter.jar:modes/java/mode/jsoup-1.7.1.jar:modes/java/mode/org.eclipse.core.contenttype.jar:modes/java/mode/org.eclipse.core.jobs.jar:modes/java/mode/org.eclipse.core.resources.jar:modes/java/mode/org.eclipse.core.runtime.jar:modes/java/mode/org.eclipse.equinox.common.jar:modes/java/mode/org.eclipse.equinox.preferences.jar:modes/java/mode/org.eclipse.jdt.core.jar:modes/java/mode/org.eclipse.osgi.jar:modes/java/mode/org.eclipse.text.jar:modes/java/mode/org.netbeans.swing.outline.jar" processing.mode.java.Commander "$@" & wait
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句