java - Unix 脚本中的 JSTAT 无法正常工作

标签 java linux unix jstat

在编写unix脚本时,我很困惑为什么脚本会出错。

脚本是这样的……

jvmpid=$(pidof java)
./jstat -gc $jvmpid

当我运行脚本时,它出错了:

Malformed VM Identifier: 3492 Usage: jstat -help|-options jstat - [-t] [-h] [ []]

Definitions: An option reported by the -options option Virtual Machine Identifier. A vmid takes the following form: [@[:]] Where is the local vm identifier for the target Java virtual machine, typically a process id; is the name of the host running the target Java virtual machine; and is the port number for the rmiregistry on the target host. See the jvmstat documentation for a more complete description of the Virtual Machine Identifier. Number of samples between header lines.
Sampling interval. The following forms are allowed: ["ms"|"s"] Where is an integer and the suffix specifies the units as milliseconds("ms") or seconds("s"). The default units are "ms". Number of samples to take before terminating. -J Pass directly to the runtime system.

但是,如果我直接在 shell 中执行脚本的每一行,每行都可以正常工作。

有什么线索吗?我已经在网上搜索过帮助。

最佳答案

我遇到了同样的问题,我解决了。问题似乎是变量 jvmpid 没有'3492'而是'3492CRLF',所以它不能被 jstat 正确理解。 我写了一个 bash 脚本,它使用 printf 将变量正确格式化为小数。 printf 在执行此操作时会出错,但会执行此操作。

JAVAPID=$(pidof java)
#Transform the pid into a valid decimal as the output of pidof has some escape characters. Also avoid printing the printf error
JAVAPID1=$(printf "%d" $JAVAPID 2> error.txt)
#Remove the error file generated by printf. Gave an error but did the job
rm -f error.txt
#Execute the command
jstat -gc $JAVAPID1

如您所见,我不是 bash 脚本专家,所以这可能可以用更简洁的方式完成,但这是我发现的最佳方式。 希望对您有所帮助。

关于java - Unix 脚本中的 JSTAT 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29955057/

相关文章:

java - 将 Java 代码格式化为 Word/RTF

java - 我应该使用 hibernate 二级缓存吗?

java - 如何制作一个特殊的 cron 作业?

linux - Bash:与查找一起使用时,重命名命令不适用于子目录

linux - 在 .so 库上使用 gprof?

c - 在 C 中使用 mount(2) 函数

c - 即使 SIGKILLed 但正常进程不会做同样的事情,终端也会与所有 child 一起出现故障

java - 选择方法解释

java - IBM i (as400) DB2 从 java 检索并解压值

linux - Linux下是否可以分配RAM的某个扇区?