Skip to content

Instantly share code, notes, and snippets.

@sar-joshi
Last active September 4, 2024 01:15
Show Gist options
  • Save sar-joshi/f86a0ada45ca2e832fd874f18b310f13 to your computer and use it in GitHub Desktop.
Save sar-joshi/f86a0ada45ca2e832fd874f18b310f13 to your computer and use it in GitHub Desktop.
Useful Linux Commands

Cron

  • Status: sudo systemctl status cron
  • Restart: sudo /etc/init.d/cron restart
  • Stop: sudo /etc/init.d/cron stop
  • Edit CronJobs: sudo vi /etc/crontab

System Status

  • Top 10 process: ps aux --sort=-%cpu | head -n 10
  • One-time snapshot of CPU usage: ps aux | sort -nrk 3,3 | head -n 5
  • Live status of a process: top -p <PID>
  • Find PID of a process by name: ps aux | grep <NAME/AnyKeyword> | grep -v grep
  • All processes with PID: ps -ef
  • All processes with PID (user-friendly format): ps -aux
  • Live status of top 10 processes: top -c -o +%CPU -n 10
    • Show the full command (-c)
    • Sort by CPU usage (-o +%CPU)
    • Display only the top 10 processes (-n 10)
    • Press 'P' (capital P) to sort by CPU usage
    • Press 'M' (capital M) to sort by memory usage
    • Adjust the update interval, you can use the -d option, example top -c -o +%CPU -n 10 -d 1 update in every 1 second
    • Note: the commands will only work for few seconds tho, so use loop logic below for continuous udate
    • To see this in a loop with a cleared screen each time (similar to the batch mode but updating), you can use:
      • while true; do clear; top -b -c -o +%CPU -n 1 | head -n 17; sleep 10; done
      • This will:
        • Clear the screen
        • Show the top 10 CPU-consuming processes with full commands
        • Wait for 10 second
        • Repeat

Vi Editor

  • Option mode: Esc key
  • Delete current line: dd (after going to option/idle mode)
  • quit: :q + enter
  • write + quit: :wq + enter
  • ignore changes: :q! + enter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment