We faced issues in our production server where apache was running as nobody and our crons were running under root and mysql under some other user permissions. The below steps were done by creating a linux user apusr and assigning mysql and apache to run under apusr permissions. Also all crons were moved to crontab under this user profile.
1. Create a new user apusr and a new group apusr.
2. Assign a ppassword for the new user apusr
3. Change the apache config file (httpd.conf)to reflect the new user and group for apache.
User apusr
Group apusr
3.1 Add user=apusr in the mysqld section in my.cnf file
And restart apache.
4. Change the ownership of the /opt folder and any other folder your application is accessing to apusr
chown -R apusr:apusr /opt /mnt
5. Remove all cron entries from root profile and add to the apusr profile.
6. Access to the production server as root should be avoided in future
Wednesday, December 22, 2010
Wednesday, February 18, 2009
Getting started with OpenNLP(Natural Language Processing)
I am not someone who have worked much on Java and so when the openNLP tools(www.opennlp.com) were in java i knew i was going to have some difficult time. On top of this the documentation for this is almost non-existent. After spending some time on the net I came across this nice article from Daniel which had code snippets to get started on NLP. He has very well explained the basic starting steps and there is no point in me repeating the steps. This is the link to Daniel's blog.
After some firefighting I have finally been able to write a sample code which can complement steps provided by Daniel.I have uploaded the source code of the same.It contains all required external libraries including maxent, opennlp-tools, opennlp and trove. Also I have included required models to parse english sentences. You can download the code from here
After some firefighting I have finally been able to write a sample code which can complement steps provided by Daniel.I have uploaded the source code of the same.It contains all required external libraries including maxent, opennlp-tools, opennlp and trove. Also I have included required models to parse english sentences. You can download the code from here
Thursday, October 16, 2008
Directory locking in perl
Directory locking in perl can be achieved by using the file locking construct flock().In the below code, only one thread can access the files contained in the directory. Once the first thread is finished, then only the other thread can start reading the contents of the directory.
use Fcntl ':flock';
chdir('orders');
open( FILE_LOCK, ">file.lock");
#Get exclusive lock on a lock file.Waits till the lock is acquired
if (flock FILE_LOCK, LOCK_EX) {
opendir(DIR, '.');
@dir_contents = readdir(DIR);
#Sleep will help you test the code. Run the same code on two different shells
sleep(5);
foreach $file (@dir_contents){
print $file."\n";
}
sleep(5);
}
close FILE_LOCK;
use Fcntl ':flock';
chdir('orders');
open( FILE_LOCK, ">file.lock");
#Get exclusive lock on a lock file.Waits till the lock is acquired
if (flock FILE_LOCK, LOCK_EX) {
opendir(DIR, '.');
@dir_contents = readdir(DIR);
#Sleep will help you test the code. Run the same code on two different shells
sleep(5);
foreach $file (@dir_contents){
print $file."\n";
}
sleep(5);
}
close FILE_LOCK;
Wednesday, October 15, 2008
DB Row locking without using inbuilt DB locks
I had come across a scenario where multiple threads would access the same row in the DB and would result in race condition. I was unable to use inbuilt lock as the mysql DB table was MyISAM. Also wanted a generic solution which could work for DBs with minimal locking mechanism.
The example is quite generic in nature and can be applied to any other language/DB.
The example is where multiple threads are trying to send the orders from the DB.
# Use process ID as our lock ID. Could really use any unique identifier.
$lock_id = getpid();
$lock_timestamp = time();
# "Claim" the orders that this process will attempt to re-upload, so that other
# processes cannot grab them - this prevents the race condition
sql("UPDATE order_table SET lock_id = $lock_id, lock_timestamp = $lock_timestamp WHERE lock_id IS NULL");
# Figure out which orders I was able to "claim"
$orders_to_retransmit = sql("SELECT * FROM order_table WHERE lock_id = $pid");
# ... process orders, if any were successful
# Clear locks - in case we still couldn't transmit, give a future process the opportunity
sql("UPDATE order_table SET lock_id = NULL WHERE lock_id = $pid");
# Extra: Clean up any abandoned locks from previous crashed runs
# (shouldn't happen but you never know)
sql("UPDATE order_table SET lock_id = NULL, lock_timestamp = NULL WHERE NOW() - lock_timestamp > 3600");
The example is quite generic in nature and can be applied to any other language/DB.
The example is where multiple threads are trying to send the orders from the DB.
# Use process ID as our lock ID. Could really use any unique identifier.
$lock_id = getpid();
$lock_timestamp = time();
# "Claim" the orders that this process will attempt to re-upload, so that other
# processes cannot grab them - this prevents the race condition
sql("UPDATE order_table SET lock_id = $lock_id, lock_timestamp = $lock_timestamp WHERE lock_id IS NULL");
# Figure out which orders I was able to "claim"
$orders_to_retransmit = sql("SELECT * FROM order_table WHERE lock_id = $pid");
# ... process orders, if any were successful
# Clear locks - in case we still couldn't transmit, give a future process the opportunity
sql("UPDATE order_table SET lock_id = NULL WHERE lock_id = $pid");
# Extra: Clean up any abandoned locks from previous crashed runs
# (shouldn't happen but you never know)
sql("UPDATE order_table SET lock_id = NULL, lock_timestamp = NULL WHERE NOW() - lock_timestamp > 3600");
Subscribe to:
Posts (Atom)