Tag Archives: Linux

Run parallel (threaded) bash commands with a job limit from file

I wanted to change mysql databases to use innodb_file_per_table problem is even if you add this to your my.cnf it will only affect newly created tables.

In order to alter existing ones I needed to run a bunch of alter tables with ENGINE=InnoDB. Because I have heaps of database and even more tables this was proving to be a huge time sink and my system was only using 1/16 of its resources since mysql only uses one thread per query.

To use multi thread power I needed to be able to run multiple queries at the same time but not too many otherwise I’ll hit the max amount of connection limit or max allowed number of forks.

Here’s the small script I wrote to overcome this problem:

# Read line in ~/tmp/convert_tables.txt
while read command; do
    # Echo with command are we running (I love stdout)
    echo $command;

    # Run the command in backgroud
    `bash -c "$command"`&

    # This is the where magic happens,
    # we don't want to have more than 10 jobs running
    # so with a simple while counting the jobs running we 
    # can put the script "on hold" until other jobs finish
    while [ `jobs | wc -l` -gt 10 ]; do
        sleep 1;
    done

done < ~/tmp/convert_tables.txt

Inside convert_tables.txt I just have the commands I want to run, here a example set:

mysql -u root -e "alter table db1.keyvalue ENGINE=InnoDB;"
mysql -u root -e "alter table db2.acl_role ENGINE=InnoDB;"
mysql -u root -e "alter table db2.acl_role_has_privilege ENGINE=InnoDB;"
...

Althout this post is about the threaded “jobs” if you’re wondering how to generate the table alter commands I follow this example:  innodb_file_per_table – Converting to Per Table Data for InnoDB

Configuring webalizer for all your sites

Webalizer is a great tool to analyse your sites traffic.

Here’s is how I’ve configured mine:

I keep my sites under /var/www/html change this according with with server configuration.

1) Install webalizer

For Debian

apt-get update && apt-get install webalizer

For CentOS

yum install webalizer

Or you could always download and compile

2) create your vhost and folder for webalizer
Here’s an example:


        DocumentRoot /var/www/html/webalizer/public_html
        ErrorLog    /var/www/html/webalizer/public_html/logs/error_log
        CustomLog    "|/usr/sbin/cronolog /var/www/html/webalizer/public_html/logs/access_log.%Y%m%d" combined
        ServerName webalizer.example.com
        ServerAlias www.webalizer.example.com

3) create this script

#!/bin/bash

DATE=`date --date='1 day ago'  +%Y%m%d`
LOGFILE="access_log.$DATE"

for i in $(ls /var/www/html/); do

cd /var/www/html/$i/logs

STATPATH=/var/www/html/webalizer/public_html/$i

if [ ! -d $STATPATH ]; then
mkdir $STATPATH
fi

webalizer -c /etc/webalizer.conf -n $i -o $STATPATH -t $i $LOGFILE

done

4) create a cronjob

0 4 * * * /storage/scripts/webstats.sh >> /dev/null 2>&1

And you’re done.

Restore spam email amavis with amavisd-release

To restore email marked as spam by amavis we going to use the command amavisd-release

First edit the amavisd-release script to ensure that $socketname is properly configured, got to be something like this:

 $socketname = '/var/vscan/amavisd.sock';

next check the log file for the serial of the mail, eg:

mavis[29297]: (29297-01-6) Blocked SPAM, ... <...> -> <...>,
  quarantine: spam/U/UM3XM3XDbN52.gz,
              ^^^^^^^^^^^^^^^^^^^^^^
  Message-ID:<...>, mail_id: UM3XM3XDbN52, Hits: 13.365,

and to recover that specific message run the command:

 amavisd-release spam-UM3XM3XDbN52.gz mail@example.com

where mail@example.com is the recipient address that will receive the recovered email

Reset / Repair linux permission

If you, like me did a chown -R…. or chmod -R…. out of the place you should done it and now your system is collapsing from the inside out, fear not, the solution is simple.

To set permissions of files in a package, enter:

rpm --setperms {packagename}

To set user/group ownership of files in a package, enter:

rpm --setugids {packagename}

And for what matter, the massive way, reset the entire file and group permission in the system:

for p in $(rpm -qa); do rpm --setperms $p; done
for p in $(rpm -qa); do rpm --setugids $p; done

Just run both and u’ll be back in business @ no time!

source

Find biggest objects in Squid-Cache

Just go to your log directory, normaly /var/log/squid and run the command:

sort -r -n +4 -5 access.log | awk '{print $5, $7}' | head -25

Purge / remove one site from Squid-Cache

To purge some site from your cache first ensure that in your squid.conf you are allowing purge, for example:

#Create ACL
acl purge method PURGE

#Aply ACL Rules
http_access allow purge localhost
http_access deny purge

After adding this lines don’t forget to run:

squid -k reconfigure

and the to remove site from cache just run the following command:

squidclient -m PURGE https://twitter.com

Increase shell history lines

Just run the following command and u will get 1000 lines of history:

echo "HISTSIZE=1000" >> ~/.bashrc

Remote shutdown Windows from Linux

Just run the following command:

net rpc SHUTDOWN -C "enter a comment to display at shutdown" -f -I x.x.x.x -U username%password

where x.x.x.x is the ip address.

To know what machines are on in a particular subnet just nmap it, like this:

nmap -sP 192.168.5.0/24 | cut -d " " -f2 > lixo.txt

And with the lixo.txt you can cicle the computers and shut them down, just run this:

for i in $(cat lixo.txt);do net rpc SHUTDOWN  -f -I $i -U user%password; done

Centos 5.3 minimal services

I always disable most of the services witch are activated by a default Centos 5.3 instalation.

Read more »

VSFTP with virtual users

This example shows how to set up vsftpd / PAM with "virtual users".
A virtual user is a user login which does not exist as a real login on the
system. Virtual users can therefore be more secure than real users, beacuse
a compromised account can only use the FTP server.
 Read more »