使用Python subprocess.check_output调用awk脚本返回非零退出状态2的问题排查求助
Exit status 2 when running an awk script usually signals syntax errors in the script or issues with execution permissions/paths. Let’s walk through actionable steps to diagnose and fix this:
1. First: Test the Script Directly in Terminal
Before diving into Python, run the awk script directly from your command line—this will show you the exact error message awk is throwing, which is way more helpful than just an exit code:
./fd-parser.awk problem.log
If there’s a syntax error, awk will tell you the line number and what’s wrong. For example, trailing commas in your print statements (we’ll get to that next) would trigger an immediate syntax error here.
2. Fix Syntax Errors in the awk Script
Looking at your END block, every print line ends with a trailing comma—this is invalid awk syntax. Remove those commas:
Bad:
print "\"translator_variables\":"translator_variables,
Good:
print "\"translator_variables\":" translator_variables
(Note the space between the string and variable for proper concatenation.)
Also, check the line where you calculate initialization:
initialization = $6 - search # note : this is not total time!!!
If the input line matching /Total time: [.0-9]+s/ doesn’t exist in problem.log, $6 will be empty, leading to a numeric conversion error. Add a guard clause to handle this:
/Total time: [.0-9]+s/ { if ($6 ~ /^[0-9.]+$/) { sub(/s/,'', $6) initialization = $6 - search } else { initialization = "null" } }
3. Ensure Script Permissions & Correct Path
- Make sure
fd-parser.awkis executable:chmod +x fd-parser.awk - If your Python script runs from a different directory than where
fd-parser.awklives, use the full path to the script in yourcmdlist. For example:cmd = ['/home/you/scripts/fd-parser.awk', 'problem.log']
4. Verify Input File Access
Double-check that problem.log exists in the working directory where your Python script runs, and that your user has read permissions for it. You can add quick checks in Python before calling subprocess:
import os if not os.path.isfile("problem.log"): raise FileNotFoundError("Could not find problem.log in current directory") if not os.access("problem.log", os.R_OK): raise PermissionError("No read permission for problem.log")
5. Validate the Shebang Line
The shebang at the top of your script (#!/usr/bin/awk -f) must point to a valid awk executable. Run which awk in your terminal to confirm the path—if awk is in /usr/local/bin/awk instead, update the shebang line accordingly.
6. Handle Non-Numeric Values Gracefully
If any of your variables (like translate, search) stay set to "null" because their matching lines aren’t in problem.log, trying to do arithmetic (translate+search+initialization) will throw an error. Add checks in the END block to default these to 0 if they’re still "null":
END { # Default null values to 0 for calculations translate = (translate == "null") ? 0 : translate search = (search == "null") ? 0 : search initialization = (initialization == "null") ? 0 : initialization total = translate + search + initialization # Print the JSON with fixed syntax print "{" print "\"translator_variables\":" translator_variables print "\"translator_derived_variables\":" translator_derived_variables # ... rest of your print lines, no trailing commas print "\"total\":" total print "}" }
Once you’ve fixed these issues, test the script again in the terminal. When it outputs valid JSON without errors, your subprocess call should work as expected.
内容的提问来源于stack exchange,提问作者ailauli69

