Thursday, 11 November 2021

ps -ef Command Cheat Sheet

## Basic Syntax and Options
1. ps -ef                    # Basic full format listing
2. ps -ef -ww               # Full format with unlimited width
3. ps -efw                  # Wide format
4. ps -ef --cols 1000       # Set output width to 1000
5. ps -ef --no-headers      # Remove header line

## Output Formatting
6. ps -ef -o pid,ppid,user,cmd    # Custom columns
7. ps -ef -o pid,ppid,user,%cpu,%mem,cmd  # With CPU and memory
8. ps -ef -o pid,ppid,user,cmd --sort=-pid  # Sort by PID
9. ps -ef -o pid,ppid,user,cmd --sort=-%cpu # Sort by CPU usage
10. ps -ef -o pid,ppid,user,cmd --sort=-%mem # Sort by memory usage

## Process Filtering
11. ps -ef | grep <pattern>        # Basic grep
12. ps -ef | grep -i <pattern>     # Case-insensitive
13. ps -ef | grep -v <pattern>     # Exclude pattern
14. ps -ef | grep -A 2 -B 2 <pattern>  # Show context
15. ps -ef | grep -E "pattern1|pattern2"  # Multiple patterns

## Process Tree
16. ps -ef --forest               # Show process tree
17. ps -ef --forest | grep <pattern>  # Filter process tree
18. ps -ef --forest -o pid,ppid,user,cmd  # Custom tree format
19. ps -ef --forest --sort=-%cpu  # Tree sorted by CPU
20. ps -ef --forest --sort=-%mem  # Tree sorted by memory

## User-Specific Commands
21. ps -ef | grep ^root          # Root processes
22. ps -ef | grep ^$USER         # Current user processes
23. ps -ef -u username           # Specific user processes
24. ps -ef -u root,username      # Multiple users
25. ps -ef | grep -E "^root|^$USER"  # Root and current user

## CPU and Memory Monitoring
26. ps -ef --sort=-%cpu | head -n 10  # Top 10 CPU processes
27. ps -ef --sort=-%mem | head -n 10  # Top 10 memory processes
28. ps -ef -o pid,ppid,user,%cpu,%mem,cmd --sort=-%cpu  # Detailed CPU
29. ps -ef -o pid,ppid,user,%cpu,%mem,cmd --sort=-%mem  # Detailed memory
30. ps -ef | awk '$3 > 50 {print}'  # High CPU usage

## Process Timing
31. ps -ef | grep "$(date +%H:%M)"  # Current hour:minute
32. ps -ef | grep "$(date +%H)"     # Current hour
33. ps -ef -o pid,ppid,user,lstart,cmd  # With start time
34. ps -ef -o pid,ppid,user,etime,cmd   # With elapsed time
35. ps -ef | grep -E "$(date +%H):[0-5][0-9]"  # Current hour range

## Process State
36. ps -ef | grep -i "zombie"      # Zombie processes
37. ps -ef | grep -i "defunct"     # Defunct processes
38. ps -ef | grep -i "sleep"       # Sleeping processes
39. ps -ef | grep -i "running"     # Running processes
40. ps -ef | grep -i "stopped"     # Stopped processes

## Port and Network
41. ps -ef | grep <port_number>    # Processes by port
42. ps -ef | grep -i "listen"      # Listening processes
43. ps -ef | grep -i "established" # Established connections
44. ps -ef | grep -i "netstat"     # Network statistics
45. ps -ef | grep -i "tcp"         # TCP processes

## Application Specific

46. ps -ef | grep java            # Java processes
47. ps -ef | grep python          # Python processes
48. ps -ef | grep nginx           # Nginx processes
49. ps -ef | grep apache          # Apache processes
50. ps -ef | grep mysql           # MySQL processes

## Process Relationships
51. ps -ef | grep -E "^[0-9]+ [0-9]+ 1 "  # Direct children of init
52. ps -ef | grep -E "^[0-9]+ [0-9]+ $PPID "  # Children of current process
53. ps -ef | grep -E "^[0-9]+ $PPID "  # Parent of current process
54. ps -ef | grep -E "^[0-9]+ [0-9]+ $PID "  # Children of specific PID
55. ps -ef | grep -E "^[0-9]+ $PID "  # Parent of specific PID

## Resource Monitoring
56. ps -ef | awk '$3 > 50 {print}'  # High CPU usage
57. ps -ef | awk '$4 > 50 {print}'  # High memory usage
58. ps -ef | awk '$3 > 50 || $4 > 50 {print}'  # High CPU or memory
59. ps -ef | awk '$3 > 50 && $4 > 50 {print}'  # High CPU and memory
60. ps -ef | awk '$3 > 50 {print $2}'  # PIDs with high CPU

## Process Cleanup

61. ps -ef | grep -i "defunct" | awk '{print $2}'  # Defunct PIDs
62. ps -ef | grep -i "zombie" | awk '{print $2}'   # Zombie PIDs
63. ps -ef | grep -i "stopped" | awk '{print $2}'  # Stopped PIDs
64. ps -ef | grep -i "sleep" | awk '{print $2}'    # Sleeping PIDs
65. ps -ef | grep -i "running" | awk '{print $2}'  # Running PIDs

## Process Analysis
66. ps -ef | grep -i "cron"        # Cron jobs
67. ps -ef | grep -i "daemon"      # Daemon processes
68. ps -ef | grep -i "systemd"     # Systemd processes
69. ps -ef | grep -i "kernel"      # Kernel processes
70. ps -ef | grep -i "init"        # Init processes

## Process Monitoring

71. watch -n 1 'ps -ef | grep <pattern>'  # Real-time monitoring
72. ps -ef | grep <pattern> | wc -l  # Count matching processes
73. ps -ef | grep <pattern> | awk '{print $2}'  # Get PIDs
74. ps -ef | grep <pattern> | awk '{print $1}'  # Get users
75. ps -ef | grep <pattern> | awk '{print $3}'  # Get PPIDs

## Process Statistics

76. ps -ef | awk '{print $1}' | sort | uniq -c  # Process count by user
77. ps -ef | awk '{print $3}' | sort | uniq -c  # Process count by PPID
78. ps -ef | awk '{print $2}' | sort | uniq -c  # Process count by PID
79. ps -ef | awk '{print $5}' | sort | uniq -c  # Process count by TTY
80. ps -ef | awk '{print $6}' | sort | uniq -c  # Process count by time

## Process Debugging
81. ps -ef | grep -i "error"       # Error processes
82. ps -ef | grep -i "fail"        # Failed processes
83. ps -ef | grep -i "crash"       # Crashed processes
84. ps -ef | grep -i "hang"        # Hung processes
85. ps -ef | grep -i "dead"        # Dead processes

## Process Management
86. ps -ef | grep -i "kill"        # Kill processes
87. ps -ef | grep -i "stop"        # Stop processes
88. ps -ef | grep -i "start"       # Start processes
89. ps -ef | grep -i "restart"     # Restart processes
90. ps -ef | grep -i "reload"      # Reload processes

## Process Security

91. ps -ef | grep -i "root"        # Root processes
92. ps -ef | grep -i "sudo"        # Sudo processes
93. ps -ef | grep -i "su"          # Su processes
94. ps -ef | grep -i "ssh"         # SSH processes
95. ps -ef | grep -i "telnet"      # Telnet processes

## Process Optimization
96. ps -ef | grep -i "high"        # High resource usage
97. ps -ef | grep -i "low"         # Low resource usage
98. ps -ef | grep -i "normal"      # Normal resource usage
99. ps -ef | grep -i "critical"    # Critical processes
100. ps -ef | grep -i "important"  # Important processes

## Best Practices
- Always use -ww or --cols for full command lines
- Use less or more for long outputs
- Use grep --color=auto for better visibility
- Save output to file for analysis
- Use --forest to understand process relationships
- Use --sort to organize output by different criteria
- Use awk for complex filtering
- Use watch for real-time monitoring
- Use custom formats for specific needs
- Use process trees for relationship analysis %    

No comments:

Post a Comment