You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派Tomcat Servlet执行Python脚本无响应问题求助

Hey Jakob, let's dig into why your Python script isn't running even though the Servlet is returning the right messages. Here are the most common culprits and fixes to check step by step:

1. 权限问题(最可能的原因)

Tomcat on Raspberry Pi usually runs under the tomcat system user, while your light.py is probably owned by your pi user or another account. If the tomcat user doesn't have execute or read permissions for the script (or its parent directory), the script won't run.

  • Test it directly: Run sudo -u tomcat python3 /full/path/to/light.py in your terminal. If you get a permission error, fix it with:
    • Add execute permissions to the script: chmod +x /full/path/to/light.py
    • Grant tomcat ownership of the script directory: sudo chown -R tomcat:tomcat /path/to/your/script/folder
2. Python Path Mismatch

When you call Python from the Servlet, you might be using a relative path (like python instead of python3) or the path to Python isn't in Tomcat's environment variables.

  • Use absolute paths: First find where Python3 lives on your Pi with which python3 (usually /usr/bin/python3). Then update your Servlet code to use this full path instead of just python.
  • Check the shebang line: Make sure the first line of light.py is #!/usr/bin/python3 (matches the absolute path you found) instead of a generic #!/usr/bin/env python3 which might not resolve correctly in Tomcat's environment.
3. Capture Script Error Output

Right now, you're only seeing the Servlet's success messages, but the Python script might be throwing errors that you're not capturing. Modify your Servlet code to log the script's output and exit code:

ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3", "/full/path/to/light.py", "close", "light");
pb.redirectErrorStream(true); // Merge error output with standard output
Process p = pb.start();

// Read and log the script's output
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
    System.out.println("Script Output: " + line); // This goes to Tomcat's logs
}

int exitCode = p.waitFor();
System.out.println("Script Exit Code: " + exitCode); // 0 = success, non-zero = error

Then check Tomcat's logs (usually in /var/log/tomcat9/ — look at catalina.out or localhost.log) for error messages from the script. Common issues here are missing Python libraries (like RPi.GPIO) or invalid GPIO references.

4. GPIO Access Permissions (if your script controls hardware)

If light.py interacts with Raspberry Pi GPIO pins, the tomcat user might not have permission to access the GPIO subsystem.

  • Fix this by adding tomcat to the gpio group:
    sudo usermod -aG gpio tomcat
    sudo systemctl restart tomcat9
    
5. Double-Check Your Servlet's Command Arguments

Make sure you're passing the close light parameters correctly to the script. If you're concatenating them into a single string, the script might not parse them properly. Instead, split them into separate arguments in ProcessBuilder:

// Correct way: each parameter is a separate string
ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3", "/full/path/to/light.py", "close", "light");

// Wrong way: treats "close light" as one single argument
// ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3 /full/path/to/light.py close light");

内容的提问来源于stack exchange,提问作者Jakob U

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:38:02