如何用绝对路径及动态端口运行sh文件?JMeter端口配置失效求解
Hey there! Let's troubleshoot why your SERVER_PORT environment variable isn't taking effect when you run your shell script. Here are a few common fixes and checks to try:
1. Use JMeter's Official -J Parameter (Recommended)
Many versions of JMeter don't actually respect the SERVER_PORT environment variable directly. Instead, they expect you to pass port overrides as a system property using the -J flag—this is the most reliable, officially supported method.
Modify your script to look like this:
/opt/jmeter/apache-jmeter-"$1"/bin/jmeter-server -Jserver_port="$2"
When you run sh shellFileName 1 1410, this will explicitly tell JMeter to use port 1410 instead of the default 1099.
2. Ensure Environment Variable is Properly Passed
Your original syntax (SERVER_PORT="$2" command) should technically set the variable for that single command, but sometimes shell behavior or the jmeter-server script's internals can interfere. Try exporting the variable first to make it available to the script and its sub-processes:
export SERVER_PORT="$2" /opt/jmeter/apache-jmeter-"$1"/bin/jmeter-server
Or use the env command to explicitly pass the variable:
env SERVER_PORT="$2" /opt/jmeter/apache-jmeter-"$1"/bin/jmeter-server
3. Debug Your Parameter Values
Double-check that your script is actually receiving the port number you're passing. Add an echo line to verify:
echo "Running JMeter version $1 on port $2" # Your jmeter-server command here
Run your script again—if the port number shows up incorrectly, you might have a typo in how you're calling the script or in the variable references.
4. Check the jmeter-server.sh Script
Take a quick look at the jmeter-server.sh file itself to see how it handles ports. Open /opt/jmeter/apache-jmeter-"$1"/bin/jmeter-server and search for lines related to port configuration. You might see something like:
if [ -z "$SERVER_PORT" ]; then SERVER_PORT=1099 fi
If this exists, your original environment variable approach should work, and the problem is likely a typo or incorrect parameter. If the script reads from jmeter.properties instead, stick with the -Jserver_port method.
内容的提问来源于stack exchange,提问作者Torikul Alam

