树莓派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:
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.pyin 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
tomcatownership of the script directory:sudo chown -R tomcat:tomcat /path/to/your/script/folder
- Add execute permissions to the script:
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 justpython. - Check the shebang line: Make sure the first line of
light.pyis#!/usr/bin/python3(matches the absolute path you found) instead of a generic#!/usr/bin/env python3which might not resolve correctly in Tomcat's environment.
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.
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
tomcatto thegpiogroup:sudo usermod -aG gpio tomcat sudo systemctl restart tomcat9
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

