Reputation: 1
Please, I am trying to create the below script:
..
do
PID=$(echo $i | cut -d: -f1)
THREADS=$(cat /proc/$PID/status | grep -i "Threads" | awk '{print $2}')
done
..
But sometimes, there are processes that do not have such file status created. So, how can I handle such exception as I am getting this message while executing the script
$ ./check_memory.sh cat: /proc/9809/status: No such file or directory $
So, I need to print this message as follows:
Memory Usage Per Process
----------------------------------------
PID THREADS
9936 129
9809 There is no status file for this process
Appreciate your support!
Appreciate your support, please
Upvotes: 0
Views: 35
Reputation: 185025
You can use this sh
script:
#!/bin/sh
cat<<EOF
Memory Usage Per Process
----------------------------------------
PID THREADS
EOF
for pid in 999 $$; do
threads=$(awk '/Threads/{print $2}' "/proc/$pid/status" 2>/dev/null) ||
threads='There is no status file for this process'
printf '%b\n' "$pid\t$threads"
done
Memory Usage Per Process
----------------------------------------
PID THREADS
999 There is no status file for this process
345234 1
#!/bin/sh
printf "%-10s%-10s%s\n" "PID" "THREADS"
function sysmon_main() {
pids=( $(ps -o pid ax | awk 'NR>1') )
for pid in "${pids[@]}"; do
threads=$(awk '/Threads/{print $2}' "/proc/$pid/status" 2>/dev/null) ||
threads='There is no status file for this process'
printf "%-10s%-10s%s\n" "$pid" "$threads"
done
}
sysmon_main | sort -bnr -k2
Upvotes: 2