$npx -y skills add chaterm/terminal-skills --skill process-managementLinux process management and control
| 1 | # Process Management |
| 2 | |
| 3 | ## Overview |
| 4 | Linux process viewing, signal handling, resource limiting and other management skills. |
| 5 | |
| 6 | ## Process Viewing |
| 7 | |
| 8 | ### ps Command |
| 9 | ```bash |
| 10 | # Common formats |
| 11 | ps aux # All process details |
| 12 | ps -ef # Full format |
| 13 | ps -eo pid,ppid,cmd,%mem,%cpu # Custom columns |
| 14 | |
| 15 | # Find specific process |
| 16 | ps aux | grep nginx |
| 17 | ps -C nginx # By command name |
| 18 | |
| 19 | # Process tree |
| 20 | ps auxf |
| 21 | pstree |
| 22 | pstree -p # Show PID |
| 23 | ``` |
| 24 | |
| 25 | ### top/htop |
| 26 | ```bash |
| 27 | # top interactive commands |
| 28 | top |
| 29 | # P - Sort by CPU |
| 30 | # M - Sort by memory |
| 31 | # k - Kill process |
| 32 | # q - Quit |
| 33 | |
| 34 | # htop (more user-friendly) |
| 35 | htop |
| 36 | ``` |
| 37 | |
| 38 | ### Other Tools |
| 39 | ```bash |
| 40 | # Sort by resource |
| 41 | ps aux --sort=-%cpu | head -10 # Highest CPU |
| 42 | ps aux --sort=-%mem | head -10 # Highest memory |
| 43 | |
| 44 | # View process details |
| 45 | cat /proc/PID/status |
| 46 | cat /proc/PID/cmdline |
| 47 | ls -la /proc/PID/fd # Open file descriptors |
| 48 | ``` |
| 49 | |
| 50 | ## Signal Handling |
| 51 | |
| 52 | ### Common Signals |
| 53 | ```bash |
| 54 | # Signal list |
| 55 | kill -l |
| 56 | |
| 57 | # Common signals |
| 58 | # SIGTERM (15) - Graceful termination (default) |
| 59 | # SIGKILL (9) - Force termination |
| 60 | # SIGHUP (1) - Reload configuration |
| 61 | # SIGSTOP (19) - Pause process |
| 62 | # SIGCONT (18) - Continue process |
| 63 | ``` |
| 64 | |
| 65 | ### kill Command |
| 66 | ```bash |
| 67 | # Terminate process |
| 68 | kill PID # Send SIGTERM |
| 69 | kill -9 PID # Force terminate |
| 70 | kill -HUP PID # Reload config |
| 71 | |
| 72 | # Terminate by name |
| 73 | pkill nginx |
| 74 | pkill -9 -f "python script.py" |
| 75 | |
| 76 | # Terminate all processes of a user |
| 77 | pkill -u username |
| 78 | |
| 79 | # killall |
| 80 | killall nginx |
| 81 | killall -9 nginx |
| 82 | ``` |
| 83 | |
| 84 | ## Background Tasks |
| 85 | |
| 86 | ### Job Control |
| 87 | ```bash |
| 88 | # Run in background |
| 89 | command & |
| 90 | nohup command & # Ignore hangup signal |
| 91 | nohup command > output.log 2>&1 & |
| 92 | |
| 93 | # Job management |
| 94 | jobs # View jobs |
| 95 | fg %1 # Foreground |
| 96 | bg %1 # Background |
| 97 | Ctrl+Z # Pause current process |
| 98 | ``` |
| 99 | |
| 100 | ### screen/tmux |
| 101 | ```bash |
| 102 | # screen |
| 103 | screen -S session_name # Create session |
| 104 | screen -ls # List sessions |
| 105 | screen -r session_name # Resume session |
| 106 | Ctrl+A D # Detach session |
| 107 | |
| 108 | # tmux |
| 109 | tmux new -s session_name |
| 110 | tmux ls |
| 111 | tmux attach -t session_name |
| 112 | Ctrl+B D # Detach session |
| 113 | ``` |
| 114 | |
| 115 | ## Resource Limits |
| 116 | |
| 117 | ### ulimit |
| 118 | ```bash |
| 119 | # View limits |
| 120 | ulimit -a |
| 121 | |
| 122 | # Set limits |
| 123 | ulimit -n 65535 # Max file descriptors |
| 124 | ulimit -u 4096 # Max processes |
| 125 | ulimit -v unlimited # Virtual memory |
| 126 | |
| 127 | # Permanent settings /etc/security/limits.conf |
| 128 | # * soft nofile 65535 |
| 129 | # * hard nofile 65535 |
| 130 | ``` |
| 131 | |
| 132 | ### cgroups |
| 133 | ```bash |
| 134 | # View cgroup |
| 135 | cat /proc/PID/cgroup |
| 136 | |
| 137 | # Limit CPU (systemd) |
| 138 | systemctl set-property service.service CPUQuota=50% |
| 139 | |
| 140 | # Limit memory |
| 141 | systemctl set-property service.service MemoryLimit=512M |
| 142 | ``` |
| 143 | |
| 144 | ## Common Scenarios |
| 145 | |
| 146 | ### Scenario 1: Find and Kill Zombie Processes |
| 147 | ```bash |
| 148 | # Find zombie processes |
| 149 | ps aux | awk '$8=="Z" {print}' |
| 150 | |
| 151 | # Find parent process |
| 152 | ps -o ppid= -p ZOMBIE_PID |
| 153 | |
| 154 | # Kill parent process |
| 155 | kill -9 PARENT_PID |
| 156 | ``` |
| 157 | |
| 158 | ### Scenario 2: Find Process Using Port |
| 159 | ```bash |
| 160 | # Find process using port 80 |
| 161 | lsof -i :80 |
| 162 | ss -tlnp | grep :80 |
| 163 | netstat -tlnp | grep :80 |
| 164 | |
| 165 | # Kill process |
| 166 | fuser -k 80/tcp |
| 167 | ``` |
| 168 | |
| 169 | ### Scenario 3: Monitor Process Resources |
| 170 | ```bash |
| 171 | # Real-time monitor single process |
| 172 | top -p PID |
| 173 | watch -n 1 "ps -p PID -o %cpu,%mem,cmd" |
| 174 | |
| 175 | # View files opened by process |
| 176 | lsof -p PID |
| 177 | ``` |
| 178 | |
| 179 | ## Troubleshooting |
| 180 | |
| 181 | | Problem | Solution | |
| 182 | |---------|----------| |
| 183 | | Process unresponsive | `strace -p PID` to view system calls | |
| 184 | | CPU 100% | `top`, `perf top` to analyze hotspots | |
| 185 | | Memory leak | `pmap -x PID`, `/proc/PID/smaps` | |
| 186 | | Zombie process | Find parent process, restart or kill parent | |
| 187 | | Process OOM killed | `dmesg | grep -i oom` | |