# Listing processes and open files - `top` shows live list of open processes every 3 sec (by default) ![[images/Pasted image 20250806201308.png]] - sorts processes by CPU usage by default - `h` for help screen - < and > shift sort column - Top CPU % Usage categories: - us = user - sy = system - ni = nice - id = idle - wa = waiting - hi = hardware interrupt - si =software interrupt - q to exit - `top -p PID` shows top info for specified process - `htop` newer version of `top` - sorts processes by memory usage by default - allows point and click with mouse - q to exit - `nice` is associated with process priority level - processes consume CPU cycles and other resources based on associated nice level - `nice -n 10 process_name &` or `nice -10 process_name` - give process a nice level of 10 and runs in background - `renice -n xxx process_name` to update nice level - increasing priority requires root prems but decreasing does not - `nice` requires root prems for nice level over 0 - highest priority = -19; lowest priority = 20 - `ps` shows instantaneous snapshot open processes with PIDs - `ps` sans options shows processes run by current shell - `ps -e` shows all processe - `ps aux` shows all processes running - `ps aux | head -n 10` shows top 10 processes running based on PID - `ps -ef` shows processes running but different columns as opposed to `ps aux` - Accepted parameter types: - Unix‐style parameters are preceded by a dash - BSD‐style parameters are not preceded by anything - GNU long parameters are preceded by a double dash - `pstree` show familial hierarchy for processes ![[images/Pasted image 20250804201558.png]] - -A for ASCII line drawings - -c for color - -n sort by PID number - -p for PIDs - `lsof` lists all open files - `lsof -u user_name | head -n 25` lists first 25 open files for `user_name` based on PID - `lsof -i` shows open networok streams with port numbers # Discovering PIDs - `pidof process_name` - requires precise process_name - `which process_name` outputs location where process is stored ![[images/Pasted image 20250802134527.png]] - `type tr` shows info associated with process ![[images/Pasted image 20250802134535.png]] - `kill -9 process_name` - requires precise match and kills all associated instances - default `kill` is sigterm or signal 15 - `pkill -9 process_name_ish` - allows partial matches and kills all associated instances - `ps aux | grep process_name` to show PID for process - alternatively `ps aux | grep -E xxx` same as `ps aux | egrep xxx` - `ps aux | pgrep process_name_ish` shows PID for process - allows partial matches for process_name # Kill Signals - ![[images/Pasted image 20250803204314.png]] - SIGTERM - 15 - please stop (try this first) - signal terminate; default kill signal         - `kill PID` or `kill -15 PID`         - graceful termination - SIGHUP - 1 - I'm leaving, whatevs (try second) - `pkill -1 process_name` - signal hang up - now it reloads the config file - SIGINT - 2 - crt+c (try second) - `kill -2 PID` - signal interrupt - SIGKILL - 9 - stop now (last resort) - `kill -9 PID` - `kill -sigkill PID` - SIGUSR1 - 10 - pssst, hey! - PID does whatever it's programmed to do in response to this signal # Process States - Running (R - `ps` representation) - Stopped (T - `ps` representation) - process is paused and placed in background - Interruptable Sleeping (S - `ps` representation) - Uninterruptable Sleeping (D - `ps` representation) - for example, waiting for available disk IO or other resources - `kill -9 PID` does not work here - Zombie (Z - `ps` representation) - represents leftover child process that still has a spot in the process table because it hasn't been cleaned up by parent process but isn't running while parent is still running - `kill -9 PID` does not work here - kill parent to remove zombie child - send KILLCHILD signal to parent, which may force parent to cleanup and thus remove zombie child - is not as troubling as the above state # Jobs & Ctrl Commands - `jobs` shows current jobs associated with current shell ![[images/Pasted image 20250803203805.png]] - default job = job with + - `fg job_number` sends job to foreground - `bg job_number` sends job to background - add `&` after a command to place in background - for example, `sleep 100 &` - still send output to terminal, for example, `ping [google.com](http://google.com) &` - `screen command_name` to start a new terminal that can be disconnected and reconnected as needed. Processes running from within the screen session do not know that they are running in a screen session and therefore meet the criteria needed to satisfy this question - `fg` and `bg` commands will not meet the criteria, and the kill command will stop a process - crtl+c = SIGINT (-2) - crtl+z = SIGSTP (-20) - puts process in stopped state in the background - crtl+d = sends EOF (end of file) - closes a terminal or text editor - does not send SIGQUIT