Friday, October 07, 2011

Ruby script to list process info in Windows

Below is a Ruby script to list process id and its command line in Windows.

To run it:
ruby pid.rb <process-name>
ie: ruby pid.rb java: will list java.exe and javaw.exe processes


class ProcessInfo
  attr_accessor :cmdLine, :pid
  
  def self.parseWmic(cmd)
    result = `#{cmd}`
    raise("Error: " + result) unless $? == 0
    processes = []
    pinfo = nil
    result.split(/\r?\n/).each do |line|
      next if line =~ /^\s*$/
      if line =~ /CommandLine=(.*)/i
        pinfo = ProcessInfo.new
        pinfo.cmdLine = $1
      elsif line =~ /ProcessId=(\d+)/i
        pinfo.pid = $1
        processes << pinfo unless pinfo.pid.to_i == $$.to_i
      end
    end
    return processes
  end
  
  def self.queryProcess(processName)
    return self.parseWmic("wmic process where \"name like '" + processName + 
                "%'\" get ProcessID,Commandline /format:list")
  end
  
  def self.queryJavaProcess
    return self.queryProcess('java')
  end  
  
  def to_s
    @pid.to_s + " " + @cmdLine
  end	
end	

puts ProcessInfo.queryProcess(ARGV[0])

Thursday, October 06, 2011

Equivalent of ps -ef | grep java in Windows

If you want to list a process and its command line, process id in Windows, this is the command:

 wmic process where "name='java.exe'" get ProcessID, Commandline /format:list

That is the equivalent of "ps -ef | grep java" in Linux. Then you could use taskkill /f /pid <PID> to kill it.

Friday, September 23, 2011

Install JDK on Windows without a UI

in CMD.exe shell:

./jdk-6u27-windows-x64.exe /s INSTALLDIR=\c:\jdk\jdk1.6.0_27-x64

This will allow you to install the JDK automatically without any GUI prompt, ideal for automation script.Note the forward slash before the c: drive is necessary.

Thursday, April 07, 2011

Force JBoss to load your lib jars

If you're running JBoss and you want it to favor librarires that go with your WAR instead of using what JBoss bundled with. Add this jboss-web.xml to your WEB-INF dir


<jboss-web>
<class-loading java2classloadingcompliance="false">
<loader-repository>
org.terracotta:archive=all-in-one.war
<loader-repository-config>
java2ParentDelegation=false
</loader-repository-config>
</loader-repository>
</class-loading>
</jboss-web>

Substitute "org.terracotta" for your package name and "all-in-one.war" for your WAR

Thursday, November 04, 2010

QA Engineer needed at Terracotta

If you're like testing softwares, writing applications to find bugs and figuring out performances, Terracotta is looking for QA engineers.

Thursday, July 15, 2010

Very bizarre problem when running JDK 32 bit on a 64 bit Linux box

I just noticed a very weird problem if you use a 32bit Sun JDK on a 64bit RedHat Linux.

The "user.home" system properties, returned by System.getProperty("user.home"), has the value of "?". Same thing happens with "user.name" property.

I'm not sure why this is but seems like a bug too me.


-bash-3.00$ /shares/jdk/hotspot1.6.0_16/bin/java ShowSystemProps | grep "user"
user.country = US
user.dir = /home/hhuynh
user.home = ?
user.timezone =
user.name = ?
user.language = en


Correct values if I used 64bit JDK


-bash-3.00$ /shares/jdk/hotspot1.6.0_16_x64/bin/java ShowSystemProps | grep "user"
user.country = US
user.dir = /home/hhuynh
user.home = /export2/homes/hhuynh
user.timezone =
user.name = hhuynh
user.language = en


This bug leads to Maven creating the local repository under the current working dir: "./?/.m2/repository" and leads to many confusing problems down the road.

Wednesday, July 14, 2010

Simple Bash script to rotate log

If you have some program that doesn't handle its own log rotation, you could use the below script to rotate the log. Ideally, you would call this script in a daily crob job.


#!/bin/bash
logfile=$1
if [ ! -f $logfile ]; then
echo "log file not found $logfile"
exit 1
fi
timestamp=`date +%Y%m%d`
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
cat /dev/null > $logfile
gzip -f -9 $newlogfile