nohup不会将任何内容写入文件

huangapple go评论95阅读模式
英文:

nohup doesn't write anything to file

问题

我有一个Java程序,它执行以下Shell脚本来重新启动自己。

    sleep 5
    nohup java -jar /home/my-dir/MyJar.jar &

如果我从终端运行脚本,它会按预期工作。但是,如果Java程序执行该脚本,程序会正常启动,但没有任何内容写入输出文件。

我通过以下代码启动脚本:

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("/bin/sh", "/home/my-dir/start.sh");
    try {
        processBuilder.start();
        logger.info("成功启动");
    } catch (IOException e) {
        e.printStackTrace();
    }
英文:

i have a Java program which executes the following shell script to restart it self.

sleep 5
nohup java -jar /home/my-dir/MyJar.jar &

If i run the script from a terminal, it just works as expected. However if the Java Program executes the script, the program starts normally but nothing gets written to the output file.

I start the script via the following code

ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("/bin/sh", "/home/my-dir/start.sh");
try {
    processBuilder.start();
    logger.info("Successfully started");
} catch (IOException e) {
    e.printStackTrace();
}

答案1

得分: 0

以下命令对我有效。它会将所有输出写入指定的文件。

nohup java -jar /home/my-dir/MyJar.jar > /home/my-dir/log.txt & tail -f /home/my-dir/log.txt &
英文:

Following command worked for me. It writes all the output to the specified file.

nohup java -jar /home/my-dir/MyJar.jar > /home/my-dir/log.txt & tail -f /home/my-dir/log.txt &

huangapple
  • 本文由 发表于 2020年4月5日 04:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61034126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定