您可以从Ubuntu的终端中使用BASH脚本。如果将脚本保存为文件名“ proc.sh”,请从命令行
bash proc.sh
#!/bin/bash
# Define output CSV file name
OUTPUT_FILE="processes.csv"
# Create/overwrite the file with headers
echo "PID,USER,CPU%,MEM%,VSZ,RSS,TTY,STAT,START,TIME,COMMAND" > $OUTPUT_FILE
# Get process information and append to CSV file
# We use ps aux and then replace spaces with commas using awk
ps aux | tail -n +2 | awk '{
# Store the command (which might contain spaces)
command = ""
for (i=11; i<=NF; i++) {
if (command != "") command = command " "
command = command $i
}
# Replace any commas in the command with spaces to prevent CSV issues
gsub(",", " ", command)
# Print the data in CSV format
printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,\"%s\"\n",
$2, $1, $3, $4, $5, $6, $7, $8, $9, $10, command
}' >> $OUTPUT_FILE
echo "Process information has been written to $OUTPUT_FILE"