Monday, October 4, 2010

To run process at background and redirecting the output/logs to a file.

For Example : To run startWeblogic.sh at background and redirect output/logs to CA1.log
and how to archive the logs.

startwls.sh
nohup /sites/WLS/DOMAINS/CA1/bin/startWebLogic.sh >/tmp/CA1_$$.log 2>&1 &
In above script, we are redirecting the logs to CA1_$pid at server start up.
Explanation:
/sites/WLS/DOMAINS/CA1/bin/startWebLogic.sh is the command used to start weblogic
To run the above command background we need use nohup
nohup /sites/WLS/DOMAINS/CA1/bin/startWebLogic.sh &
The above command makes the startWeblogic.sh command to run background and the logs will be redirected to nohup.out
But if we use above command, we cant start run another process backgroud with nohup because nohup.out is used by another process

So for this purpose
We use " > /tmp/CA1.log 2>&1".
> is used to redirect logs
&1 is our /tmp/CA1.log and 2>&1 to redirect standard error to stdout (i.e 1).

So the command is -
nohup /sites/WLS/DOMAINS/CA1/bin/startWebLogic.sh >/tmp/CA1.log 2>&1 &

But by command, if server is restarted previous logs are gone.

To archive previous logs, we can use pid (process id). Inorder to achieve that we can append process id variable ($$)
So total command is
nohup /sites/WLS/DOMAINS/CA1/bin/startWebLogic.sh >/tmp/CA1_$$.log 2>&1 &

or else to be precise u can also append date with time
temp_$(date +%m%d%y)_$(date +%H%M%S).log

 if we have logs with pid attached, how to view the latest/current log.
 For example:
$ls -ltr *CAMAIN1*
-rw-r--r--   1 wlsadmin atg       490353 Sep 29 08:50 PQACAMAIN1logs.txt.2128172119
-rw-r--r--   1 wlsadmin atg       371140 Sep 29 16:37 PQACAMAIN1logs.txt.5029085048
-rw-r--r--   1 wlsadmin atg       537022 Oct  1 15:13 PQACAMAIN1logs.txt.3729163730
-rw-r--r--   1 wlsadmin atg       387844 Oct  4 13:41 PQACAMAIN1logs.txt.2301172351

showlogs.sh
less -i /tmp/$(ls -ltr /tmp/*CAMAIN1* | sed s/^.*PQA/PQA/g | tail -1)
or
by using awk
less -i $(ls -ltr /tmp/*CAMAIN1* | tail -1 | awk '{print $9}')
By above script, we will "less" the latest CA1*.log.

Explanation:
ls -ltr /tmp/*CAMAIN1* - list all the logs with CAMAIN1 (listing from least edited to recently edited).
sed s/^.*PQA/PQA/g - used sed to get exact filename.
tail -1 - to list the last line
awk '{print $9}' - print 9th column of the row

Using pipe '|" run all the above commands parllely
Once u got the name of the file open using less command. (-i is to ignore case while searching).

No comments:

Post a Comment