Friday, March 30, 2012

Linux Commands

How To Find Out My Linux Distribution Name and Version
cat /etc/*-release

Labels:

Send email through PuTTY



echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" yourname@domain.com

Labels:

Monday, March 19, 2012

Define variables in Struts 1.3


<bean:define id="bookActivityKey">${job.bookActivity.bookActivityKey}</bean:define>
<logic:equal name="bookActivityKey" value="9">
<![CDATA[1]]>
</logic:equal>
<logic:notEqual name="bookActivityKey" value="9">
<![CDATA[0]]>
</logic:notEqual>

Labels: ,

Thursday, March 15, 2012

Restart Tomcat via Shell Script Using Crontab


Here is the shell script to shutdown and start Tomcat

#!/bin/bash
#Important!! Script Must be run as root(you can ignore this if your tomcat does not need root
PATH=$PATH:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ant/bin:/opt/java/
export PATH
NOW=$(date +"%Y%m-%dT%H%M")

if [ "$(id -u)" != "0" ];
then
echo "This script must be run as root" 1>&2
exit 1
fi

echo `date` ":=================================" >> /tmp/tomcat-restart.log
echo `date` ":Tomcat restart script initiated!!" >> /tmp/tomcat-restart.log
echo `date` ":=================================" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
echo `date` ":Tomcat is running" >> /tmp/tomcat-restart.log

kill -3 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
/opt/tomcat/bin/catalina.sh stop
echo `date` ":Tomcat has shutdown" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
kill -9 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
fi

#echo "Restarting tomcat !!!!!" >> /tmp/tomcat-restart.log
#/opt/tomcat/bin/catalina.sh start
#else
#echo "Tomcat not found running" >> /tmp/tomcat-restart.log
#echo "Restarting tomcat !!!!!" >> /tmp/tomcat-restart.log
#/opt/tomcat/bin/catalina.sh start
fi

echo `date` "Sleep started" >> /tmp/tomcat-restart.log
sleep 30
echo `date` "Sleep End" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -eq 0 ];
then
/opt/tomcat/bin/catalina.sh start
echo `date` "Tomcat restarted." >> /tmp/tomcat-restart.log
fi

Notes:
  • `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l`  - This returns number of active tomcat6 processors
  • If it is grater than '0', tomcat6 is running and has that number of active processors.
  • If it is '0', tomcat6 is not running
What you need to do:
  • Copy and past this into your text editor and save it as restart-tomcat.sh
  • Copy restart-tomcat.sh into /usr/bin/
  • Go to /usr/bin/ and change the file permission to 0777
    • cd /usr/bin/
    • #chmod 0777  restart-tomcat.sh
  • Change these in  restart-tomcat.sh according to your environment
    • PATH 
    • tomcat6 
    • /opt/tomcat/bin/catalina.sh stop 
    • /opt/tomcat/bin/catalina.sh start 
  • If your crontab does not contains JRE_HOME=/opt/java, script might complain when you run  restart-tomcat.sh. If complains, add it.
  • You can run this script as follows;
    • ./restart-tomcat.sh 
    • This will create a log in  /tmp/tomcat-restart.log
    • If there's any errors, you can find it in /var/spool/mail/root file. Do this to see the errors.
      • cd /var/spool/mail
      • tail -f root
  • If everything goes well, create a crontab record
    • crontab -e (to edit the crontab)
    • This record trigger the job at 10:15 AM everyday
      • 15 10 * * * /usr/bin/restart-tomcat.sh
That's it!

Labels:

Tuesday, March 13, 2012

Start, Stop and Restart Crond Daemon in Linux


Cron jobs in Linux and its variants such as FreeBSD or CentOS which commonly used on web hosts due to popularity of cPanel web server control panel system, is important time based scheduled tasks and commands that perform various functions such as log rotation, updates checking, system maintenance, file system cleaning, backup, service restart and etc.

Cron jobs are managed by a daemon namedcrond. When cron schedules are added, deleted or modified by crontab, any changes are enacted by the crond daemon. Crond daemon runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. If any, crond will execute the commands. If crond process is not running, no cron jobs will be executed.
Thus, it’s important to ensure that crond daemon is running and not hanged in the system. To manage crond daemon in Linux, we can make use of “service” command.
To check the status of crond daemon:
service crond status
To stop and terminate crond process:
service crond stop
To start and run crond daemon:
service crond start
To restart crond service:
service crond restart

Labels:

Monday, March 12, 2012

Create Apache POI Custom Color


Excel template path
String excelTemplate = "C:/Export-Generic.xls";

Create HSSFWorkbook
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelTemplate));

Create HSSFPalette (Represents a workbook color palette.)
HSSFPalette palette = workbook.getCustomPalette();

Sets the color at the given offset
Parameters:
index the palette index, between 0x8 to 0x40 inclusive
red the RGB red component, between 0 and 255 inclusive
green the RGB green component, between 0 and 255 inclusive
blue the RGB blue component, between 0 and 255 inclusive
Note: As I know of default colors occupy 0-40. Therefor, custom colors needs start at 41. Custom color that I've chosen is #CADDFF it's RGB values are R = 202, G = 221, and B = 255. http://html-color-codes.info helped me to get the RGB colors.

palette.setColorAtIndex(new Byte((byte) 41), new Byte((byte) 202), new Byte((byte) 221), new Byte((byte) 255));


Now we have custom color palette. We can use this to set our style.
Create style
HSSFCellStyle style = workbook.createCellStyle();

Apply the our custom color to cell background
style.setFillForegroundColor(palette.getColor(41).getIndex());


Happy Coding :-)

Labels: ,

Thursday, March 8, 2012

Mapping tomcat host in server.xml


<Host name="me.mysite.com" appBase="webapps">
<Context path="" docBase="/opt/tomcat6/webapps/support"/>
        <Logger className="org.apache.catalina.logger.FileLogger"/>
</Host>

Labels: ,

Wednesday, March 7, 2012

Java Date


// Get today as a Calendar
Calendar today = Calendar.getInstance();
// Subtract 1 day
today.add(Calendar.DATE, -1);
// Make an SQL Date out of that
java.sql.Date yesterday = new java.sql.Date(today.getTimeInMillis());

Labels: