Suppose you want to know how many files are in the current directory.
ls | wc -l
Suppose you want to know about the five processes that are consuming the most CPU time on your system:
ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6
The ps command’s o lets you specify the columns that you want to be shown. sort -r does a reverse order sort with the second column (pcpu) as reference (k2). head gets only the first six lines from the ordered list, which includes the header line. You can place pcpu as the first column and then omit the k2 option because sort by default takes the first column to do the sort.
Read the rest of this entry »
Like this blog? Why not buy me a cup of coffee?
Filed under:
Linux, Scripting
Here is little perl script created by rhai of pinoytux.com on how to rotate apache log files.
#!/usr/bin/perl
$DIR=”/var/www/html/sites/yoursite.com/logs”;
$DATE=`date +%F`;
chomp $DATE;
@log = `/usr/bin/find $DIR -type f -name “*.log” `;
#print “\nRotating $DATE\n”;
foreach $log (@log) {
chomp $log;
$new_fn=”$log”.”-”.”$DATE”;
#print “$log $new_fn\n”;
`mv “$log” “$new_fn”`;
`gzip -9 $new_fn`;
}
Like this blog? Why not buy me a cup of coffee?
Here is a small perl script to check if your site is under DDOS attack. I use this script to check our web server for certain attacks.
#!/usr/bin/perl
%ip_add=();
@list=`netstat -an |grep :80 |grep EST |awk ‘{print \$5}’ |awk -F: ‘{print \$1}’`;
foreach $line(@list) {
chomp($line);
$ip_add{$line}++;
}
$total=0;
foreach $line2(keys (%ip)) {
$total+=$ip{$line2};
print “$line2=$ip{$line2}\n”;
}
print “total=$total\n”;
— After you get a cheap web hosting deal, you should first go to the hosting product site to learn about it. Unlike others, bluehost as well as hostgator come with their own set of directions. —
Like this blog? Why not buy me a cup of coffee?
Filed under:
Perl, Scripting
This will replace all occurrences of match_pattern in the file
:g/match_pattern/s//replace_string/g
ex: you want to change all occurrences of an allow rule in your vhost file and comment it out
Allow from 192.168.0.1 to #Allow from 192.168.0.1
:g/Allow from/s//#Allow from/g
This command will find and replace the string that matches the condition.
alternatively, you can use ‘ : ‘ instead of ‘ / ‘ to make path (/) slashes easier to manage (no escape (\) character needed)
:g:match_pattern:s::replace_string:g
Like this blog? Why not buy me a cup of coffee?