我有以下脚本:
#!/bin/bash
until java -jar "MyApplication.jar"; do
echo "---"
date
echo "Application crashed with exit code $?. Waiting to respawn..." >&2
sleep 60
echo "Respawning.." >&2
done如果java应用程序不以退出代码0终止,它基本上将重新启动。
我目前调用该程序的方法如下:
nohup ./myapplication.sh &其中myapplication.sh是包含上述脚本的文件。
我在X11下从给定的xterm调用这个程序。我希望确保程序继续运行,即使我关闭了xterm,甚至终止了整个X11会话。
与此有关的两个问题:
是否可以在脚本中包含nohup,所以我只需要这样做:./myapplication.sh
myapplication_20191118_1723.out的文件(即时间戳的名称和时间戳-format没有那么相关),而不是默认的nohup.out?该文件应同时包含std out.和std err。发布于 2019-11-17 20:45:41
nohup实际上并没有做多少有价值的事情。
如果最初连接到TTY,则
/dev/null。nohup.out,如果它们最初连接到TTY,则f 210
所有这些事情你都可以自己做,根本不用nohup。(此外,非交互式shell在默认情况下不会传播HUP!)。
#!/bin/bash
# Redirect stdin from /dev/null, and stdout and stderr to a log file
# (not having TTY handles is part of how/why programs started with nohup can survive
# a terminal dying).
#
# Note that printf %(...)T is a bash 4.3 feature
# ...you may need to use date if your bash is older.
printf -v logfile_name 'myapplication_%(%Y%m%d_%H%M%S)T' -1
exec </dev/null >"$logfile_name" 2>&1
# ignore any HUP signals we receive (even though we shouldn't get any regardless)
trap '' HUP
# prevent the JVM from getting any handles on X11, which would be broken on exit
unset DISPLAY
until java -jar "MyApplication.jar"; do
retval=$?
echo "---" >&2
date >&2
echo "Application crashed with exit code $retval. Waiting to respawn..." >&2
sleep 60
echo "Respawning.." >&2
donehttps://stackoverflow.com/questions/58905011
复制相似问题