Path: content/Tips/Linux/default.md
Linux
Simple Parallelism
Sometimes you just want to limit the number of background tasks in a script. Here's an easy way if you don't want to refactor it using parallel:
while read line
do
some_command --with $line >> log.file &
while [ $(jobs | wc -l) -gt 50 ]; do
sleep 2;
done
done < source
The inner while loop will sleep until there are less than 50 tasks in the queue
Local firewalling
To block an incoming IP address:
iptables -A INPUT -s 187.3.166.114 -j DROP
Or a particular port:
iptables -A INPUT -s 65.55.44.100 -p tcp --destination-port 25 -j DROP
The -s (ip address) is optional in this case.
You can also pipe in data from ipdeny.com, eg:
wget -O- --no-check-certificate https://www.ipdeny.com/ipblocks/data/countries/ru.zone | \
while read i; do
iptables -A INPUT -s$i -j DROP
done
Who logged on last?
Normal people use the 'last' command. You can also use this monstrosity if you are feeling hackish:
perl -we '$recs = ""; while (<>) {$recs .= $_};$uid = 0;foreach (split(/(.{292})/s,$recs)) {next if length($_) == 0;my ($binTime,$line,$host) = $_
=~/(.{4})(.{32})(.{256})/;if (defined $line && $line =~ /\w/) {$line =~ s/\x00+//g;$host =~
s/\x00+//g;printf("%5d %s %8s %s\n",$uid,scalar(gmtime(unpack("I4",$binTime))),$line,$host)}$uid++}print"\n"' < /var/log/lastlog
note - this didn't work the last time I tried t (in 2023)
Soak testing servers
Simple, safe way to raise system load:
NUMCPU=$(grep $'^processor\t*:' /proc/cpuinfo |wc -l)
for ((i=0;i<$NUMCPU;i++));do
echo 'scale=100000;pi=4*a(1);0' | bc -l &
done ;\
sleep 60; \
killall bc
That brings the cpu load up for 60 seconds by calculating pi to absurd presision, thousands of times. Note the test to see how many threads it should run in parallel.
'stress' is available on Ubuntu platforms which can also test CPU, memory and disk load.
Somewhat hostile way to load up a system:
:(){ :|:& };:
(from http://www.cyberciti.biz/faq/understanding-bash-fork-bomb/ ) This can take the LA over 1000 so no running it on a live server!
Using find
The options -delete and -ls are a useful optimisation, saves running '-exec '{}' ls -la \;' and is
also a lot faster as it doesn't have to spawn an extra process per file.
Unix Bar Charts
Little perl script to turn a stream of numbers into a pretty realtime barchart:
root@host:~# function baa
{
perl -ne '$min=0; $max=6750; $w=75; use POSIX; $d=ceil(log($max)/log(10)); $w-=$d; $v=$_<$min?0:$_>$max?$max:$_;
$s=$w*$v/($max-$min); $bar=join("", ("*")x$s); $bar=~s/.$/|/ if $v==$max; print sprintf("%${d}d ",$_)."$bar\n";'
}
then, something like this:
(while [ 1 ] ; do
mailq | tail -1 | awk '{ print $5;}'; sleep 1; done) | baa
will generate this:
1700 *****************
1720 ******************
2000 *********************
2040 *********************
2060 *********************
1640 *****************
Strip last column of text file
To strip the last column of a file, use awk like this:
cat file | awk -F. 'NF{NF--};1'
-F. : use '.' as the column separator
Links in this section
ClawsMail
Desktop
Filesystems
SSH
Systemd
VI
htmailread
Last updated : 17 December 2025