Riz Khan on May 15th, 2011

I will be posting a series of posts on scripting starting from some basic commands, control  structures, small scripts and then moving to some complete practical scripts for general server  automation, monitoring, reporting and backups which I would hope one can follow in sequence  and learn scripting.

This would be first one in this series of posts starting with a basic string/character translation  command tr.

tr is a very useful command that helps us to operate on characters and perform different functions on them. The tr command is part of the coreutils package/rpm.

root@centos[~]# which tr
root@centos [~]# rpm -qf /usr/bin/tr
root@centos [~]#

It can be used to perform functions like changing the case of characters e.g. from upper to lower case or vice versa, delete, complement, replace or squeeze repeat characters etc. Lets look at some basic examples.

Below is an example of how we can use it to change the case of characters :

root@centos [~]# echo STRINGINCAPS1 | tr A-Z a-z
root@centos [~]#
root@centos [~]# echo stringinsmallletters1 | tr a-z A-Z
root@centos [~]#

The output and usage is pretty much obvious, so I will leave it to you to test it on your end. Lets look another example of squeezing repeating characters in a string :

root@centos [~]# echo REPEEEATchars | tr -s A-Z
root@centos [~]#

As you can see the repeated ‘E’ in the string have been squeezed to single character after translation by using -s switch with the tr command.

These are just some basic examples to get you started, you can learn more options available, using commands tr –help and man tr. We can e.g. use the tr command in loop to change case of all files in a directory from caps to lower case or vice versa. Or required character replacements can be performed using -c switch.

In the next post I will discuss the cut command which is a very powerful string parsing command that is used very commonly in the bash scripts to parse and then select certain section/part from some output.

Tags: , ,

By compiling PHP with mssql extension support one can have capability to connect to MS SQL server from a PHP Linux environment.

For enabling mssql support in PHP we need FreeTDS installed and configured which is a set of Unix/Linux libraries that implement the TDS protocol.

This post explains the steps for doing this, lets get started.

First we will need to download and install FreeTDS, you can find more information and download link at http://www.freetds.org URL. Note the exact installation steps below :

cd /usr/local/src
wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
tar -xzvf freetds-stable.tgz
cd freetds-*/
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld
make install

Once the installation is complete we will add the related FreeTDS libraries and binaries installation path to the dynamic linker binding config files at /etc/ld.so.conf , we will add below two paths :


Edit the config file using vi or editor of your choice to add above paths and then use ldconfig command to build dynamic links for the shared libraries :

vi /etc/ld.so.conf
ldconfig -v

You can check connectivity to external MS SQL server using below command :

tsql -S [servername] -p [port]  -U [username] -P [password]

Standard MS SQL port is 1443 which you can use above. Also if you want to have VPS/Server level connectivity configuration for MS SQL you can configure the/usr/local/freetds/etc/freetds.conf file for that.

Once done , we will proceed with rebuilding PHP using the /scripts/easyapache script on the cPanel servesr/vps. For a none-cpanel installation same config parameter can be used during configure step for compiling PHP.

We need to add the configure parameter –with-mssql=/usr/local/freetds in file /var/cpanel/easy/apache/rawopts/all_php5 . Once done, save the file and proceed with easyapache script to rebuild PHP.


If you are on a 64bit platform , you may recieve this error :

configure: error: Could not find /usr/local/freetds/lib64/libsybdb.a|so

You will need to create a symlink from the original location of the libaray, the path can vary depending on the platform. In my case I noticed the file was located at normal lib directory under freetds i.e. it was located at /usr/local/freetds/lib , so I simply created a symlink named lib64 to the oringal directory lib as follows :

cd /usr/local/freetds
ln -s lib lib64
ldconfig -v

Also added the path /usr/local/freetds/lib64 to the ld.so.conf file and build the dynamic linking as done above.

Once easyapache script completes successfully, you will find mssql support enabled in PHP. You can verify that using below command :

php -i | grep mssql

Now we will create/install a copy of mssql.so libaray which we can then enable in the php.ini file. On cPanel system this can be achieved by following below steps :

cd /home/cpeasyapache/src/php-5.2.17/ext/mssql/
make install

This will install a the mssql.so library at the extension_dir path mentioned in php.ini file located at /usr/local/lib/. To enable the extension add below link in php.ini file :


Save the file and restart Apache.

Thats it !

Tags: , , ,

SolusVM is a very good control panel for OpenVZ platform and supports Xen too. It is reasonably stable and very cost affective. If you have not tried or used it yet, I would recommend giving it a try.

This post is about a recent issue I faced on SolusVM/OpenVZ node setup.

After setting up a new node on OpenVZ and SolusVM platform recently, I was informed by techs      setting up new VPSs that the VPS creation is failing and the VPSs do not start after the SolusVM    completes setting up the vps.

When I troubleshooted the issue, I found following errors in the logs :

Apr 16 18:07:34 openvzsolus kernel: VZ QUOTA: file softlimit exceeded for id=130
Apr 16 18:07:34 openvzsolus kernel: VZ QUOTA: file softlimit expired for id=130
Apr 16 18:14:00 openvzsolus kernel: VZ QUOTA: file softlimit exceeded for id=130
Apr 16 18:14:00 openvzsolus kernel: VZ QUOTA: file softlimit expired for id=130

The reason for this error is that some of the values for VPS creation are read from the sample conf file vps.basic.conf-sample which is located at /etc/vz/conf/ and has some quota values set to low values.

If you face this error you can resolve this by following below steps :

vi /etc/vz/conf/ve-vps.basic.conf-sample

After opening the file with vi editor or any other editor of your choice search for the section that states “# Disk quota parameters (in form of softlimit:hardlimit)” and update the diskspace and diskinodes parameters as below :


The default values are lower and they cause the error listed above in the post. There is no hard and fast rule for what values are best or ideal, you can adjust them according to your requirement, platform, server density and specs.

Tags: , , , , ,

I have been setting up a test server few days back by setting up MySQL , Apache and PHP from source. Though the installation all went smooth, but when I restarted apache it failed and when I checked the error logs they showed the following error :

Syntax error on line xxx of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/libphp5.so into server: /usr/local/apache/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied


I checked the related line in httpd.conf configuration file for possible syntax error , but I found none. The libphp5.so was available on the correct path and I verified the permissions and they were all correct as well. This was a new error so I had to do some googling to find some clue. What I found was that this permission error is actually related to Selinux settings which will need to be modified for the libphp5.so library.

If you face this error you can use below chcon command to change the Selinux security context for the file :

chcon -t textrel_shlib_t /usr/local/apache/modules/libphp5.so

This should resolve the problem and apache should start up fine. If you continue to see any issues you can try changing the Selinux mode from enforcing to permissive, even if that do not work you can change Selinux mode to disabled unless your environment do not allow do that.

Tags: , ,

Riz Khan on March 21st, 2011

Linux has powerful set of shell commands using which you can almost achieve anything. Specially combining commands through pipes and redirection can make them more stronger.

If you need to find the total file count in a directory in Linux ( CentOS / RHEL etc ) then you can do this using following command :

find . -type f | wc -l

or :
find ./ -type f | wc -l

Both commands will have same affect and will count all the files in all sub directories in the current directory. The parameter ‘-type f’ makes sure that ‘find’ command is only searching for files and not including directories in the count. The output is then piped ( passed ) to next command ‘wc’ called ‘word count’ and using -l or –lines parameters we can get the count for any listing. wc command has other parameters using which you can find byte count, char count etc. Check command help or man pages for detail syntax and usage.

If you just want to know the count of files in the current directory without including any subdirectories then you can achieve this using below command :

ls -l | wc -l

These commands are also helpful in general scripting and can help achieve desired logic in the scripts.

Tags: , , , , ,

Riz Khan on March 19th, 2011

Core dump is a memory or a storage dump which records the memory state of a computer program at specific instance when a program terminates abnormally. The abnormal termination of the program can be result of various reasons, however most OS are set to generate a core dump of memory state of the executable at time of failure.

They can be used for debugging by admins and mostly by software developers for the related applications, however in a shared hosting environments they are rarely used and only thing they are doing is taking additional disk space, which is problematic both for accounts users and servers admins.

It is recommended to disable them on such environments as they can always be enabled again if required for any specific debugging purpose.

They can be disabled using the limits.conf file , which is the configuration file for the pam_limit module. Below command can be used to disable the core dumps.

echo '* soft core 0' >> /etc/security/limits.conf

or alternatively you can open the configuration file in some editor , I like vi :
vi /etc/security/limits.conf

And then add below line in the configuration file :
* soft core 0

This will make sure that the core dumps are disabled on the server and are no more generated for any crashed applications.

You can use below command to find all the core files and delete them after examining :
find /home/*/public_html -name core.[0-9]*

There is a rare chance of some files with similar naming convention and being legitimate files, so it is recommended not to remove all core files automatically, rather you can view and check the results of above find command and then remove the core files manually.

The search location above is for a typical cPanel hosting servers, you can modify it according to your hosting or server environment.

Tags: , , ,