KP Solutions

Solutions for Day to Day Technical Problems

September 30, 2008
by Ketan Patel
0 comments

How to add a user to a group?

I keep on forgetting this command, so I put it here to remember it forever. To add a user ‘ketan’ to the group ‘admin’, do the following

gpasswd -a ketanĀ  admin

Such a simple and each to use command!!!

September 29, 2008
by Ketan Patel
0 comments

How to create SVN repository?

This post will help you create a new SVN repository and it assumes you have the SVN installed & configured on your computer/server.

To create the repository for first time, issue the following command

svnadmin create –fs-type fsfs /var/www/subversion/kpsolution

Then to import your code for first time, go to your base directory of the source tree and then issue the following command

svn import -m “Initial Import” . file:///var/www/subversion/kpsolution/

Now, you have imported your source code into the repository and then use the SVN GUI to checkin/checkout the changes.

For more detailed instruction on setting up the svn, see “http://www.rockfloat.com/howto/gentoo-subversion.html”

September 29, 2008
by Ketan Patel
0 comments

mysqld.log grows too big in size

Recently on one of our new servers, we had an issue where the mysql daemon had halted on bus error. We couldn’t stop the mysql daemon using

/etc/init.d/mysqld stop

Then I looked at the error-log file for mysql in /var/log/mysql-error.log and found that the disk didn’t had any empty space left and correctly enough, when I did

df -h

I saw that the /var mounted partition didn’t had any diskspace remaining. To find the exact culprit I looked around in the /var directory and found that the ‘mysqld.log’ file was 9GB large. I was puzzled as to how come this log grew so big. Thoughts came that the server would have been compromised but the actual culprit was the my.cnf file for the mysql. During the setup of the server, by mistake I had put a statement

[mysqld]

log=/var/log/mysqld.log

This configuration was causing to save all the queries that were performed on the mysql server since it booted. Naturally on a high traffic site, you will see this file grow huge quickly. I learned the lesson hard way, but always RTFM when you are configuring something on the production server. So for solution, all I had to was remove the mysqld.log file and comment out the above “log” configuration in the my.cnf.

I thought to post this out there to help other people in case they have the same issue.

June 3, 2008
by Ketan Patel
0 comments

How to create a simple linux device driver

Here, I would show you how to create a simple linux driver that does nothing! This just a demo to show you how to compile, install and remove the device driver.

Before we get started, you need to make sure that you have the kernel source installed. The following instructions will guide you through the steps to install the kernel source for RedHat Enterprice 4 (RHEL4). Issue the following command to get the kernel source.up2date –get-source kernel

This would dump the kernel-<version>.src.rpm in the /var/spool/up2date folder. Then you must install the source using the following command.

rpm -Uvh kernel-<version>.src.rpm

This would install the kernel source in the /usr/src/redhat. You need to change the directory to /usr/src/redhat/SPECS/ and issue the following command.

rpmbuild -bp –target=<arch> kernel-2.6.spec where <arch> is the desired target architecture.

Then copy the configuration file from the /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9/configs/kernel-<version>.config to
/usr/src/redhat/BUILD/.config

Then issue the following command

make oldconfig

Build all the modules before you could compile your linux device drivers.

make modules

Now that we have the proper kernel source and modules compiled, create a file ‘nothing.c’ as follows:

#include <linux/module.h>

MODULE_LICENSE(“DUAL BSD/GPL”);

and save it your home folder ~/nothing/nothing.c

In ~/nothing, create Makefile as follows:

obj-m    := nothing.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

default:
   $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

Change directory to ‘~/nothing‘ and then issue the ‘make‘ command, this will create the ‘nothing.ko’ file in the current folder.

You can load the module in the kernel space using the following command.

insmod nothing.ko

and remove the module using the command.

rmmod nothing

May 14, 2008
by Ketan Patel
0 comments

How to remove all comments in a file

If you are ever stuck in situation where you want to remove all the comments regardless of what they are in a source file. Here’s a small command that you can issue in VIM. It is for pattern search and replace using regex.

Open the file in VIM then

:g/\/\/.\+/s

Above command will look for anything after including the “//” characters which is basically a comment in C, C++, php and several other languages.

August 23, 2007
by Ketan Patel
4 Comments

How to clear sendmail queue

If you ever felt the need of flushing the sendmail queue then this post will be handy to you.

How to flush sendmail queue under linux sendmail mailq command in linux how to flush mail queue in sendmail under linux

If u r worried about sendmail pending mail flush do the following 2 things.

1) manually method –> delete /var/spool/mail/*.* files in this dir –> delete /var/mqueue/*.* files

then check if all mail gone using mailq command. all mail will be deleted.

2) using command:

use simple command sendmail -v -q in root prompt. it will flush all pending mails. rest to confirm u can run mailq command if all are really gone !!….

3) if u want perticular domain or user or recepitience mail to delete use this command

sendmail -qS -v test.com it will delete all mail from *@test.com

sendmail -qR -v hotmail.com it will delete all mail from recepient of hotmail….

Cheers!!

Ketan

July 29, 2007
by Ketan Patel
0 comments

php header() function not working!

I was going nuts to find the problem why did the header() function didn’t redirect my page in some instance and did in others! Luckily I found this function ‘headers_sent()’ which will tell you if at any given point any headers were sent. If the headers were sent from some other file then this function will give you the file name and the line number from where the unexpected header was sent. Very useful and neat. See the example below, it is from php.net manual page.

Consider the following scenario,

<?php
// Sign out
signOut();

// Redirect back to the main page
header(“Location: http://toronto.eclassifieds4u.com/main);
?>

If for some reason, the header() call to redirect is not working, there won’t be any error messages, making it difficult to debug. However, using headers_sent(), you may easily find the source of the problem.

<?php
// Sign out
signOut();

/*
* If headers were already sent for some reason,
* the upcoming call to header() will not work…
*/
if(headers_sent($file, $line)){
// … where were the mysterious headers sent from?
echo “Headers were already sent in $file on line $line…”;
}

// Redirect back to the main page
header(“Location: http://toronto.eclassifieds4u.com/main);
?>

As the documentation states, “header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.” In the above debugging solution, you will find out exactly where any of these problematic output or blank lines exist. You should then be able to resolve the issues with much more ease than if you hunted for the problems aimlessly.

May 10, 2007
by Ketan Patel
0 comments

Fatal error: Unable to read X bytes in file.php

If you got this error Fatal error: Unable to read X bytes in file.php, then:

You have encoded your php files using Zend Encoder and uploaded to the server but gives you this error. This problem occurs due to your FTP software which has ftp’ed your file to the server in ascii mode.

To fix, make sure you upload the file in binary mode and not ascii. This will fix your problem.

April 20, 2007
by Ketan Patel
0 comments

MySQL not loaded in phpinfo()

This instruction is for LAMP technology. For WAMP, I will post later on.

When you have mysql installed in your system, but you do not have mysql.so loaded in your php.ini file. Search for mysql.so in your php extensions directory. If you find it, then simply add mysql.so reference in php.ini. But if you can not find it then remove mysql installation from your system.
After you have removed MySQL completely, look for the php-mysql-5.1.6.rpm in your rpm repository. What this rpm will do is essentially install proper mysql shared object and put the reference to mysql.so in your php.ini.

Make sure you restart Apache server, otherwise the change will not take into effect.

April 12, 2007
by Ketan Patel
1 Comment

How to edit HEX file in VI?

Open the hex file or any file that you want to edit in hex format in “vi“. Lets call that file “myFile.hex”.

To open the file in “vi“, type in terminal “vi myFile.hex

After the file is opened in the “vi“, make sure you are in the command mode. If you are not sure, just hit “ESC” couple of times and it should then be in command mode.

Now type the following “:%!xxd“.

Voila, you are editing the file in hex mode now! Isn’t this awesome?

If you want to revert back to regular editing mode instead of hex mode, then make sure you are in command mode & then type the following “:%!xxd -r“.

This will bring you back to normal editing mode. Isn’t this easy?

Let me know if you have some tricks saved in your sleeves, we can share it here with others.