Friday, February 27, 2009

Cron Job Sandbox

Check out this Web site for your crob job schedule settings.

http://www.cronsandbox.com/

Although some advanced crontab setting may work in this sandbox, they may not work in some versions of Linux. The one failed to work properly in my CentOS 5.2 is:

22 7 1-7 * 2

This setting should mean the first Tuesday every month, but actually it is launched on the first day of every month.

For this type of sophisticated scheduling, I ended up using an external Windows box running plink.exe to remote the cron job.

Linux Service Check and Start Script

This script copied from somewhere checks and restarts Linux service in necessary.

======================================
#!/usr/bin/perl

# if your prgram has the string "grep" in the name or in the path
# this program won't work.

$pro2check = "service name";

open PROS, "ps -ef|grep $pro2check |";

while ($line = ){
unless ($line =~ m/grep/){
#print "it is running\n";
exit;
}
}

#print "it isn't running\n";
exec "service $pro2check start";

========================

Wednesday, February 25, 2009

Tomcat Reverse Proxy

There are two Apache Tomcat connectors can be used Tomcat reverse proxy: mod_jk and mod_proxy_ajp.

mod_proxy_ajp is new and easy to configure while mod_jk is just the opposite. I would not explain the details how to use them since this type of info can be easily found online. I just want to share what I found after weeks of frustration:
  • Use mod_proxy_ajp when your network connection between the proxy and Tomcat is very stable, and the traffic load is low.
  • Otherwise use mod_jk.
  • By default, Tomcat does NOT recycle abandoned ajp connections. You need to enable that, otherwise, broken network connections between proxy and Tomcat will max out ajp thread pool on Tomcat side.
Below settings in works.properties file is helpful in mod_jk configuration:

worker.gui2.socket_timeout=10
worker.gui2.socket_keepalive=True
worker.gui2.reply_timeout=500
worker.gui2.prepost_timeout=10000
worker.gui2.connect_timeout=10000
worker.gui2.retries=5
worker.gui2.connection_pool_timeout=600
If everything fails to maintain stable connection, disable connection reuse in Apache .conf file:

JkOptions +DisableReuse
To enable Tomcat recycle abondaned ajp threads, add:
connectionTimeout="600000"

to server.xml file.

Wednesday, February 4, 2009

Enterprise Wiki

A good candidate for wiki in company environment is Foswiki, which is a very good fork from TWiki.

Foswiki has cooperation, commenting, fine grained access control and many other more features corporate users like right out-of-box.

The downside is, like TWiki, it is based on CGI and Perl, and need tight integration with Apache, so it is a bit more complicated to set up. The performance is not optimum as well compared to PHY/MySQL counterparts.

In enterprise environment, it is an obvious better choice then Mediawiki.

Batch script to change FTP password

Changing password for FTP users accounts requires entering FTP commands in command mode. This is usually to much for end users who need to change their FTP passwords by themselves. I wrote a batch file script to make the job easier. Save the content below in a .bat file and run it.

If it does not work again your FTP server, check the syntax of

literal SITE CHPW %old_password% %new_password%

Your FTP server may use a different syntax.

@echo off

echo This program changes password on ftp.domain.com.
echo.
SET /P username=Enter FTP Username:
SET /P old_password=Enter Old FTP Password:
SET /P new_password=Enter New FTP Password:
SET /P new_password2=Enter New FTP Password again:

If not %new_password%==%new_password2% goto bad

echo %username%> ftpcmd.txt
echo %old_password%>> ftpcmd.txt
echo literal SITE CHPW %old_password% %new_password%>> ftpcmd.txt
echo bye>> ftpcmd.txt
echo.
echo ************** System Message Begins ********************************
ftp -i -s:ftpcmd.txt ftp.domain.com
echo ************** System Message Ends **********************************
del ftpcmd.txt

echo.
echo From the System Message above, you can see whether the password has been
echo changed successfully or not. If successful, the new direct access URL is:
echo.
echo ftp://%username%:%new_password%@ftp.domain.com
echo.
echo NOTE:
echo To copy URL to clipboard: Right-click, Mark, select, enter.
echo.
echo.
echo This window will close.
pause
goto end

:bad
echo.
Echo New passwords dismatch.
echo.
echo This window will close.
pause

:end

Ensure a stable mod_proxy_ajp connection

Apache Tomcat connector mod_proxy_ajp is recommended Tomcat frontend for Tomcat backend server. It provides better flexibility, scalability and security.

When using a firewall between machine running mod_proxy_ajp and backend Tomcat, the firewall may silently drop connections between the two when the connection is idle, causing following error:

(104)Connection reset by peer: ajp_ilink_receive() can't receive header

The symptom on the user side is when opening a page, usually after a long period of inactivity, it gets a 503 Service Temporarily Unavailable Error. A simple page reload will get the page back, since a new connection is initiated.

To avoid this, add a Keepalive parameter to mod_proxy_ajp to use OS TCP/IP KeepAlive function to send keepalive patches through the firewall:

ProxyPass /path_if_any ajp://backend_tomcat_server:8009/path_if_any keepalive=on