Pertemuan 5

Manajemen Proses di Sistem Operasi Linux

Monitoring, kontrol, dan optimasi proses sistem

Tujuan Pembelajaran

Setelah menyelesaikan praktikum ini, mahasiswa mampu:

  • Memahami konsep proses dan manajemen proses dalam sistem operasi
  • Menggunakan perintah untuk memantau dan menganalisis proses yang berjalan
  • Mengelola proses (start, stop, pause, resume) dengan perintah yang tepat
  • Mengidentifikasi dan menangani proses yang bermasalah (zombie, runaway)
  • Mengatur prioritas proses menggunakan nice dan renice

Teori Pendukung

Konsep Dasar Proses
Process ID (PID)
Nomor unik identifikasi proses
Parent PID (PPID)
PID proses pembuat
Owner
User yang menjalankan proses
Priority
Prioritas eksekusi CPU
Process States (Status Proses)
Status Kode Deskripsi Contoh
Running R Proses sedang dieksekusi CPU Program aktif
Sleeping S Proses menunggu event Menunggu I/O
Stopped T Proses dihentikan sementara Debugging
Zombie Z Proses telah selesai tapi entry masih ada Process cleanup needed
Jenis-jenis Sinyal (Signals)
Sinyal Numeric Default Action Keterangan
SIGTERM 15 Terminate Permintaan terminasi yang graceful
SIGKILL 9 Terminate Paksa terminasi proses (tidak bisa di-handle)
SIGSTOP 19 Stop Menjeda proses
SIGCONT 18 Continue Melanjutkan proses yang dihentikan
SIGHUP 1 Terminate Hang up (reload configuration)
SIGINT 2 Terminate Interrupt dari keyboard (Ctrl+C)
Jenis Proses
User Processes
  • Dijalankan oleh user biasa
  • PID biasanya > 1000
  • Contoh: browser, text editor
  • Berjalan di foreground/background
Daemon Processes
  • Proses sistem yang berjalan di background
  • PID biasanya < 1000
  • Contoh: sshd, nginx, mysql
  • Dimulai saat boot

Monitoring Processes

1. ps - Process Status
ps [options]

# Opsi penting:
ps # Proses di terminal saat ini
ps aux # Semua proses dengan detail
ps -ef # Full format listing
ps -u username # Proses user tertentu
ps -p PID # Proses dengan PID tertentu

# Contoh dengan filter dan sorting:
ps aux --sort=-%cpu | head -10 # 10 proses CPU tertinggi
ps aux --sort=-%mem | head -10 # 10 proses memory tertinggi
ps aux | grep nginx # Cari proses nginx
2. top - Real-time Process Monitoring
top [options]

# Shortcuts dalam top:
q # Quit
k # Kill process (masukkan PID)
r # Renice process
M # Sort by memory usage
P # Sort by CPU usage
1 # Show individual CPU cores

# Batch mode (untuk scripting):
top -bn1 # Single iteration batch mode
3. htop - Enhanced Process Viewer
# Install htop (jika belum ada)
sudo apt install htop # Ubuntu/Debian
sudo yum install htop # RHEL/CentOS

htop [options]

# Fitur htop:
Mouse support # Bisa klik kolom untuk sort
Tree view # Tampilan hierarki proses
Color-coded # Warna untuk different resource types
Search # Cari proses

# Contoh penggunaan:
htop -u username # Proses user tertentu
htop -p PID1,PID2 # Monitor proses tertentu
4. pidof & pgrep - Find Process ID
pidof process_name
pgrep process_name

# Contoh:
pidof nginx
pgrep -u root # PID semua proses root
pgrep -f "pattern" # Cari berdasarkan command line

Process Management

1. kill - Mengirim Sinyal ke Proses
kill [options] PID
kill -s SIGNAL PID
kill -SIGNAL PID

# Contoh:
kill 1234 # Default: SIGTERM (15)
kill -9 1234 # SIGKILL (force terminate)
kill -TERM 1234 # SIGTERM
kill -HUP 1234 # SIGHUP (reload config)
2. pkill & killall - Kill by Name
pkill [options] pattern
killall [options] process_name

# Contoh:
pkill nginx
pkill -9 chrome # Force kill chrome
pkill -u username # Kill semua proses user
killall firefox
killall -v process_name # Verbose output
3. nice & renice - Process Priority
Nice Value Range: -20 (highest priority) to 19 (lowest priority)
nice -n priority command
renice priority -p PID

# Contoh:
nice -n -10 ./cpu_intensive_script.sh # High priority
nice -n 10 ./background_task.sh # Low priority
renice -5 1234 # Ubah priority proses yang berjalan
renice 15 -u username # Ubah priority semua proses user
4. nohup - Run Process After Logout
nohup command &

# Contoh:
nohup ./long_running_script.sh > output.log 2>&1 &
nohup python3 server.py > server.log 2>&1 &

Langkah-langkah Praktikum

A. Persiapan Environment dan Proses Contoh
# Login sebagai root
sudo -i

# Jalankan beberapa proses contoh di background
sleep 300 &
ping -c 100 localhost > /dev/null &
tail -f /var/log/syslog > /dev/null &

# Buat script CPU intensive
cat > /tmp/cpu_test.sh << 'EOF'
#!/bin/bash
while true; do
  echo "CPU test process running..." > /dev/null
done
EOF

chmod +x /tmp/cpu_test.sh
/tmp/cpu_test.sh &
B. Monitoring Processes dengan ps
# Lihat proses yang berjalan di terminal saat ini
ps

# Lihat semua proses yang berjalan
ps aux

# Analisis output ps aux
ps aux --sort=-%cpu | head -10 # 10 proses paling banyak menggunakan CPU
ps aux --sort=-%mem | head -10 # 10 proses paling banyak menggunakan memory

# Cari proses tertentu
ps aux | grep sleep
ps aux | grep ping
ps aux | grep cpu_test
C. Real-time Monitoring dengan top dan htop
# Gunakan top untuk monitoring real-time
top
# Tekan 'q' untuk keluar, 'M' untuk sort by memory, 'P' untuk sort by CPU

# Install dan gunakan htop (lebih user-friendly)
sudo apt install htop -y # Ubuntu/Debian
sudo yum install htop -y # Almalinux/RHEL

htop

# Filter proses tertentu di htop
htop -p $(pgrep sleep | tr '\n' ',' | sed 's/,$//')
D. Mengelola Proses dengan kill, pkill, killall
# Dapatkan PID dari proses sleep
ps aux | grep sleep
PID_SLEEP=$(pgrep sleep | head -1)
echo "PID sleep: $PID_SLEEP"

# Kirim sinyal TERM (terminate gracefully)
kill $PID_SLEEP

# Kirim sinyal KILL (force terminate)
sleep 600 & # Jalankan sleep lagi
PID_SLEEP=$(pgrep sleep | head -1)
kill -9 $PID_SLEEP

# Gunakan pkill untuk terminate by name
ping -c 1000 localhost > /dev/null &
pkill ping

# Gunakan killall untuk terminate semua proses dengan nama tertentu
sleep 300 &
sleep 400 &
killall sleep
E. Mengatur Prioritas Proses dengan nice dan renice
# Jalankan proses dengan nice value yang berbeda
nice -n -10 /tmp/cpu_test.sh & # Proses dengan prioritas tinggi
PID_HIGH_PRIORITY=$!

nice -n 10 /tmp/cpu_test.sh & # Proses dengan prioritas rendah
PID_LOW_PRIORITY=$!

# Verifikasi nice value
ps -l -p $PID_HIGH_PRIORITY
ps -l -p $PID_LOW_PRIORITY

# Ubah prioritas proses yang sedang berjalan dengan renice
renice -5 $PID_LOW_PRIORITY
renice 15 $PID_HIGH_PRIORITY

# Verifikasi perubahan
ps -l -p $PID_HIGH_PRIORITY,$PID_LOW_PRIORITY
F. Menangani Proses Zombie dan Troubleshooting
# Identifikasi proses zombie
ps aux | grep defunct
ps aux | awk '$8=="Z" {print $2}' # List PID zombie processes

# Monitor resource usage secara detail
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10

# Lihat proses tree
pstree
pstree -p # Dengan PID

# Monitoring I/O processes
sudo apt install iotop -y
iotop
G. Advanced Process Management
# Menggunakan nohup untuk proses yang tetap berjalan setelah logout
nohup /tmp/cpu_test.sh > /tmp/cpu_test.log 2>&1 &

# Menggunakan screen atau tmux untuk session management
sudo apt install screen -y
screen -S long_running_process
# Jalankan proses dalam screen session
/tmp/cpu_test.sh
# Detach dengan Ctrl+A D
# Reattach: screen -r long_running_process

# Membuat service sederhana dengan systemd
cat > /etc/systemd/system/test-service.service << 'EOF'
[Unit]
Description=Test Service for Practicum
After=network.target

[Service]
Type=simple
ExecStart=/tmp/cpu_test.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start test-service
systemctl status test-service

Advanced Process Monitoring

1. Process Tree dengan pstree
pstree
pstree -p # Dengan PID
pstree -u # Dengan username
pstree -a # Dengan command arguments
pstree -A # ASCII characters
2. Detailed Process Information
# Lihat environment variables proses
cat /proc/$PID/environ | tr '\0' '\n'

# Lihat memory map
pmap $PID

# Lihat open files
lsof -p $PID

# Lihat process limits
cat /proc/$PID/limits
3. strace & ltrace - Process Tracing
strace command # Trace system calls
strace -p $PID # Trace running process
strace -c command # Summary statistics

ltrace command # Trace library calls
ltrace -p $PID # Trace running process
4. System Resource Monitoring
# Memory usage
free -h
cat /proc/meminfo

# CPU usage
mpstat
cat /proc/loadavg

# I/O statistics
iostat
vmstat

Tugas dan Evaluasi

  1. Apa perbedaan antara proses zombie dan orphan process?
  2. Kapan sebaiknya menggunakan SIGTERM dan kapan menggunakan SIGKILL?
  3. Jelaskan apa yang terjadi ketika kita mengubah nice value suatu proses!
  4. Bagaimana cara mengidentifikasi proses yang menggunakan resource CPU berlebihan?
  5. Buat skenario: Sebuah proses menjadi tidak responsif dan menggunakan 100% CPU. Tulis langkah-langkah troubleshooting yang sistematis!

Troubleshooting Script

Performance Troubleshooting Script
cat > /usr/local/bin/performance_check.sh << 'EOF'
#!/bin/bash
# Script untuk identifikasi penyebab server lambat
echo "=== SYSTEM STATUS ==="
uptime
echo ""

echo "=== TOP 5 CPU PROCESSES ==="
ps aux --sort=-%cpu | head -6
echo ""

echo "=== TOP 5 MEMORY PROCESSES ==="
ps aux --sort=-%mem | head -6
echo ""

echo "=== ZOMBIE PROCESSES ==="
ps aux | grep defunct
echo ""

echo "=== I/O STATISTICS ==="
iostat -x 1 1
EOF

chmod +x /usr/local/bin/performance_check.sh
Process Monitoring Dashboard
cat > /usr/local/bin/process_monitor.sh << 'EOF'
#!/bin/bash
# Real-time process monitoring dashboard
while true; do
  clear
  echo "=== PROCESS MONITOR $(date) ==="
  echo "Load Average: $(cat /proc/loadavg | awk '{print $1,$2,$3}')"
  echo ""
  echo "--- Top CPU Processes ---"
  ps aux --sort=-%cpu | head -10
  echo ""
  echo "--- Top Memory Processes ---"
  ps aux --sort=-%mem | head -10
  sleep 5
done
EOF

Best Practices Process Management

Yang Harus Dilakukan
  • Monitor system load average secara berkala
  • Gunakan SIGTERM sebelum SIGKILL
  • Set appropriate nice values untuk background tasks
  • Gunakan nohup/screen untuk long-running processes
  • Monitor zombie processes dan clean up jika perlu
  • Document process dependencies
Yang Harus Dihindari
  • Jangan kill process tanpa memahami dependencies
  • Jangan set nice value -20 untuk user processes
  • Jangan biarkan zombie processes menumpuk
  • Jangan ignore high load average warnings
  • Jangan run critical processes tanpa monitoring
  • Jangan gunakan SIGKILL sebagai first option
Load Average Interpretation
Load Average Interpretasi Tindakan
< CPU cores System normal No action needed
≈ CPU cores System busy Monitor closely
> CPU cores System overloaded Investigate immediately
>> CPU cores System severely overloaded Emergency action required