Linux-Schnipsel
Nur simple Basics, keine Raketenwissenschaft.
- Check which processes are using the most memory
- Top 10 processes using swap
- ssh breaks find-loop in shellscript
- Scan Network for a Service
- PID-File in bash-script
- Scrollen im Screen
- Kill process using grep
- get external-ip
- postfix - recipient address regex rewrite
- crontab
Check which processes are using the most memory
with percentages:
ps aux --sort=-%mem | head -n 10
More specifically, with RSS (Resident Set Size in KB):
ps -eo pid,user,comm,%mem,rss --sort=-rss | head -n 10
with RSS in MB:
ps -eo pid,user,comm,%mem,rss --sort=-rss | \
awk 'NR==1 {print $0,"MB"} NR>1 {printf "%-8s %-10s %-20s %-6s %.2f MB\n",$1,$2,$3,$4,$5/1024}' | head -n 10
awk 'NR==1 {print $0,"MB"} NR>1 {printf "%-8s %-10s %-20s %-6s %.2f MB\n",$1,$2,$3,$4,$5/1024}' | head -n 10
Top 10 processes using swap
smem -s swap -r | head -10
Without smem command:
for pid in /proc/[0-9]*; do
swap=$(awk '/^Swap:/ {sum+=$2} END {print sum}' "$pid/smaps" 2>/dev/null)
if [ "${swap:-0}" -gt 0 ]; then
printf "%10d %10d KB %s\n" \
"$(basename "$pid")" \
"$swap" \
"$(cat "$pid/comm" 2>/dev/null)"
fi
done | sort -k2 -nr | head -10
swap=$(awk '/^Swap:/ {sum+=$2} END {print sum}' "$pid/smaps" 2>/dev/null)
if [ "${swap:-0}" -gt 0 ]; then
printf "%10d %10d KB %s\n" \
"$(basename "$pid")" \
"$swap" \
"$(cat "$pid/comm" 2>/dev/null)"
fi
done | sort -k2 -nr | head -10
ssh breaks find-loop in shellscript
#!/bin/bash
DIR=/testdir
find ${DIR} -maxdepth 1 -type f | while read FILE ; do
# ohne </dev/null wird die Schleife nach dem ersten ssh beendet
ssh servername "hostname;" </dev/null
done
DIR=/testdir
find ${DIR} -maxdepth 1 -type f | while read FILE ; do
# ohne </dev/null wird die Schleife nach dem ersten ssh beendet
ssh servername "hostname;" </dev/null
done
Scan Network for a Service
nmap -p 22 --open -sV 192.168.0.0/24
Scans 192.168.0.1 - 192.168.0.254 for ssh-server (port 22)
You don't need root access for this
PID-File in bash-script
PID_FILE=/var/run/scriptname.pid
if [ -f $PID_FILE ]
then
PID=$(cat $PID_FILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process is already running!"
exit 1
else
## PID-File exists but process not running -> Update PID-File
echo $$ > $PID_FILE
if [ $? -ne 0 ]
then
echo "Error creating PID-file"
exit 1
fi
fi
else
echo $$ > $PID_FILE
if [ $? -ne 0 ]
then
echo "Error creating PID-file"
exit 1
fi
fi
# ..... rest of the script
# at the end of the script, remove PID-File
rm $PID_FILE
if [ -f $PID_FILE ]
then
PID=$(cat $PID_FILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process is already running!"
exit 1
else
## PID-File exists but process not running -> Update PID-File
echo $$ > $PID_FILE
if [ $? -ne 0 ]
then
echo "Error creating PID-file"
exit 1
fi
fi
else
echo $$ > $PID_FILE
if [ $? -ne 0 ]
then
echo "Error creating PID-file"
exit 1
fi
fi
# ..... rest of the script
# at the end of the script, remove PID-File
rm $PID_FILE
Scrollen im Screen
- Press STRG-A
- Press ESC
- Scroll with UP and DOWN arrow keys
- Press ESC to Exit scrollback-mode
Kill process using grep
kill $(ps -ef | grep 'search' | grep -v grep | awk '{print $2}')
orps -ef |grep 'search' | grep -v grep | awk {'print $2'} | xargs kill
orpkill -f search
-f The pattern is normally only matched against the process name. When -f is set, the full command line is used.
get external-ip
curl ifconfig.me
postfix - recipient address regex rewrite
rewrite localpart like google - remove +something from localpart
name+something@domain.tld will go to name@domain.tld
/^(.*)\+(.*)@eigeneMaildomain.ltd$/ ${1}@eigeneMaildomain.ltd
postmap /etc/postfix/recipient_canonical
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical
systemctl restart postfix
crontab
* * * * * <auszuführender Befehl>
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── Wochentag (0-7, Sonntag ist 0 oder 7)
│ │ │ └────── Monat (1-12)
│ │ └──────── Tag (1-31)
│ └────────── Stunde (0-23)
└──────────── Minute (0-59)
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── Wochentag (0-7, Sonntag ist 0 oder 7)
│ │ │ └────── Monat (1-12)
│ │ └──────── Tag (1-31)
│ └────────── Stunde (0-23)
└──────────── Minute (0-59)
Beim reboot bzw. nach dem booten ausführen:
@reboot <auszuführender Befehl>
Folgend Schreibweisen sind in der crontab auch möglich:
@daily <auszuführender Befehl>
@midnight <auszuführender Befehl>
@hourly <auszuführender Befehl>
@weekly <auszuführender Befehl>
@monthly <auszuführender Befehl>
@annually <auszuführender Befehl>
@yearly <auszuführender Befehl>
@midnight <auszuführender Befehl>
@hourly <auszuführender Befehl>
@weekly <auszuführender Befehl>
@monthly <auszuführender Befehl>
@annually <auszuführender Befehl>
@yearly <auszuführender Befehl>
Alle Ausgaben nach /dev/null umleiten:
* * * * * /pfad/script.sh > /dev/null 2>&1