ITeye博客

check if program running in linux

forrest420 · 2011-10-20 17:58 · 代码模板 · 操作系统 · shell · Markdown

In shell,we use the below program to check if some program already running: export HOMEDIR=/../../ export JAVA_HOME=/usr/lib/jvm/java-1.6.0 export LOGFILE=/tmp/log_date +"%Y%m%d-%H%M%S".log export JAVA_CMD=$JAVA_HOME/bin/java export CLASSPATH=$JAVA_HOME/lib/tools.jar:$HOMEDIR/lib/ojdbc-14.jar:$HOMEDIR/lib/log4j-1.2.14.jar:$HOMEDIR/lib/commons-logging-1.1.jar export MAIN_PROGRAM=com.test.theClassEntry ps -ef | grep "java" > SomeFile num=$(wc -l SomeFile | awk '{print $1}') if [ $num -gt 0 ] then echo "the java process is running" > $LOGFILE exit 1 else nohup $JAVA_CMD -Xmx10g -Dflag=true -classpath "$CLASSPATH" $MAIN_PROGRAM program_parameter > $LOGFILE 2>&1 & fi

note: 1.-Dproperty=value Set a system property value.You can call the following from anywhere in the code to read the properties given on the command line: String value = System.getProperty("key", "defaultvalue"); 2. http://www.linfo.org/ps.html An alternative set of options for viewing all the processes running on a system is ps -ef | less The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option. 3. http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/ Awk Example Print only specific field.

Awk has number of built in variables. For each record i.e line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.

$ awk '{print $2,$5;}' employee.txt Thomas $5,000 Jason $5,500 Sanjay $7,000 Nisha $9,500 Randy $6,000

$ awk '{print $2,$NF;}' employee.txt Thomas $5,000 Jason $5,500 Sanjay $7,000 Nisha $9,500 Randy $6,000 In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using $NF also, where $NF represents last field. In the print statement ‘,’ is a concatenator.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。