Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Created July 5, 2024 14:02
Show Gist options
  • Save iTrooz/382a094853448d491fe6d6f157c623a8 to your computer and use it in GitHub Desktop.
Save iTrooz/382a094853448d491fe6d6f157c623a8 to your computer and use it in GitHub Desktop.
OLD BUGGY IMPLEMENTATION - Show processes lifecycle in real time
#!/bin/sh
get_running_processes() {
processes=$(ps -eo ppid,pid,cmd)
echo "$processes" |
grep -v "\[.*\]" | # Filter out kernel processes
awk -v pid="$$" '$1 != pid' | # Remove children of current script
awk '{print $1 " " $2 " " $3}' | # Remove ppid + command params
sort
}
draw_left() { # Takes stopped processes as argument
tput cup 0 0
echo "Stopped Processes:"
for process in $1; do
echo $process
done
}
RIGHT=$(( $(tput cols) / 2 ))
draw_right() {
tput cup 0 $RIGHT
echo "Created Processes:"
i=1
for process in $1; do
tput cup $i $RIGHT
echo $process
i=$((i + 1))
done
}
IFS=$'\n'
running_processes=$(get_running_processes)
while true; do
# Wait for 1 second
sleep 1
# Get the updated list of running processes
updated_processes=$(get_running_processes)
# Process to get the wanted data
stopped_processes=$(comm -13 <(echo "$running_processes") <(echo "$updated_processes"))
created_processes=$(comm -23 <(echo "$running_processes") <(echo "$updated_processes"))
# Print to screen
clear
draw_left "$stopped_processes"
draw_right "$created_processes"
# Update list
running_processes=$updated_processes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment