0

When writing a unix script, I am very confused why the script is erroring out.

The script is this...

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

When I run the script, it errors out:

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.

However, if I execute each line of the script inside the shell directly, every works fine.

Any clues? I've searched the web for help already.

BobcatBlitz
  • 177
  • 7

1 Answers1

1

I faced the same problem and I sorted it out. The problem seems to be that the variable jvmpid does not have '3492' but '3492CRLF', so it is not correctly understood by jstat. I have written a bash script that uses printf to format the variable correctly into a decimal. printf gives an error doing this, however does the job.

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

As you can see I am not an expert in bash scripting, so this can probably be done in a cleaner way, but it is the best way that I found. Hope it helps.

rafaleon15
  • 11
  • 2