开发者

Java option -XX:OnOutOfMemoryError does not work when called through exec in bash script

开发者 https://www.devze.com 2023-03-30 21:02 出处:网络
I have a java application and I want to run a script whenever it experiences and OutOfMemoryException

I have a java application and I want to run a script whenever it experiences and OutOfMemoryException

This works great:

$ java -server -XX:OnOutOfMemoryError="./oom_script %p" TestOOMClass

Unfortunately my application is run b开发者_C百科y a bash script in production. The script boils down to this:

cmd='java -server  -XX:OnOutOfMemoryError="./oom_script %p"  TestOOMClass'
##does a lot of checking and cmd building here
exec -a app  ${cmd}

When run like this java never respects the double quotes and thinks %p is the class. how do I prevent this? I've tried double escaping but that doesn't work.


Since your program is run as a shell script, I would suggest putting this as the first line in your shell script after the shebang:

set -xv

Then, in the crontab, put 2>&1 at the end of the command line, so STDERR and STDOUT are merged. Crontab usually emails out the STDOUT of a command to root, so you can see what the output is. If not, then apend the following to the end of the command in your crontab:

> /somedir/output.$$ 2>&1

Make sure somedir exists, and after crontab runs your command, you'll see the verbose and debug output. Each line in your shell script will be displayed before it is executed -- both as written and as the shell actually interprets it.

The set -xv becomes very useful in debugging any sell script. There could be all sorts of environmental issues involved between the cronjob and the script running under your login. You might even find a shell issue. For example, crontab usually executes shell scripts in Bourne shell and you probably have Bash or Kornshell as your default shell. Whatever it is, you'll usually find out the issue very quickly when you turn on verbose/debug mode.

You don't even have to do this to the entire script. You can put set -xv anywhere in your script to turn on verbose/debug mode, and set +xv to turn it off.

I could make several pious high minded recommendations (use quotes, don't assume environment things, prefix your command line with "bash -c" to make sure you're using the right shell, etc.), but this would be guessing what could be wrong. In order to really debug this issue, I would need to see the machine, know the OS, see your entire shell script, and understand the entire environment. And, the first thing I would do is add set -xv in your shell script.


Quotes and escaping is an art. I would suggest you add echo ${cmd} before calling exec so you can see what it looks like then.

I would suggest using

cmd='java -server  -XX:OnOutOfMemoryError=\\"./oom_script %p\\"  TestOOMClass'

instead (untested). You need it to look like \" when being echoed.


an alternative i suggest (to bypass the problem, not solve it indeed) is to rung and bash script and access the $PPID:

   PPID   The process ID of the shell's parent.  This variable is readonly.

then kill the process with that ID (please bare in mind that is an untested suggestion)

0

精彩评论

暂无评论...
验证码 换一张
取 消