Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We've setup a cron-based invocation of a Java program every minute. This Java program invocation is written in a shell script and is setup as cron job. Each invoked Java program is directed to its own separate log file (using date & time as file name with precision upto minutes) as shown below:

DATE=`date +%Y-%m-%d_%H-%M`
/usr/java/jdk1.6.0/bin/java -Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError MyJavaProgram 2>&1 >> $DATE.log | tee -a $DATE.err >> $DATE.log &

For example, I can find out the currently running Java process (MyJavaProgram) at any time using the following command:

[root@user ~]# ps -ef |grep MyJavaProgram
user    4321     1  0 Oct17 ?        00:00:17 /usr/java/jdk1.6.0/bin/java -Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError MyJavaProgram
user    5747     1  0 Oct17 ?        00:00:11 /usr/java/jdk1.6.0/bin/java -Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError MyJavaProgram

My question is, I want to find out the actual log file name to which the currently executing Java process (MyJavaProgram) is writing to. So, for example, if there are 2 MyJavaProgram currently executing, I want to know the log file name of each process. In this case, I'm interested/want to know the .log extension file, though there are 2 extensions - .log and .err.

NOTE: Log file name is generated in this pattern, 2011-10-17_19-28.log, for example. Our server is RHEL4.

share|improve this question
up vote 2 down vote accepted

The command "lsof" might help you out here.

lsof | grep $PID |grep .log

or similar should yield you with a list of files that process is accessing.

share|improve this answer
Another way of doing the same thing is ls -l /proc/$PID/fd/*.log You can also make the above command more efficient by using lsof -p $PID | grep '\.log' – Ladadadada Oct 18 '11 at 9:50
@Antitribu: If there are multiple MyJavaProgram Java process currently running, how do I pass PID dynamically to lsof command? – Gnanam Oct 18 '11 at 10:01
@Gnanam , you mean something like for i in `ps aux |grep java |grep -v grep |awk '{print $2}'`; do echo $i; lsof |grep $i |grep \.log; done ? Flavor to taste – Antitribu Oct 18 '11 at 10:10
@Antitribu: Actually, I didn't intend to mean/ask anything specifically, but I'm just trying to find out whether multiple $PID can be passed dynamically to lsof command. – Gnanam Oct 18 '11 at 10:50
@Gnanam all good, lsof just gives you a list of open files that you can parse any way you wish. – Antitribu Oct 18 '11 at 11:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.