You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#!/usr/bin/env bash |
|
|
|
|
|
# Get the top CPU-consuming process, excluding kernel threads and this script |
|
|
read -r NAME CPU < <( |
|
|
ps -eo comm,pcpu --sort=-pcpu --no-headers 2>/dev/null | |
|
|
awk '{ |
|
|
# Skip kernel threads (names in brackets) |
|
|
if ($1 ~ /^\[/) next |
|
|
# Skip this script and ps itself |
|
|
if ($1 == "top.sh" || $1 == "ps") next |
|
|
# Output the first matching line and exit |
|
|
print $1, $2 |
|
|
exit |
|
|
}' |
|
|
) |
|
|
|
|
|
if [ -z "$NAME" ]; then |
|
|
echo '{"text": " --", "tooltip": "No process data"}' |
|
|
exit 0 |
|
|
fi |
|
|
|
|
|
# Round CPU to integer |
|
|
CPU=$(printf "%.0f" "$CPU") |
|
|
|
|
|
# Truncate long process names (keep bar readable) |
|
|
if [ ${#NAME} -gt 10 ]; then |
|
|
NAME="${NAME:0:9}…" |
|
|
fi |
|
|
|
|
|
echo "{\"text\": \"<span size='x-large' rise='-1500'></span> $NAME ${CPU}%\", \"tooltip\": \"Top CPU: $NAME — ${CPU}%\"}"
|
|
|
|