How to find out the log file name that a currently running process is writing to?

10,559

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:
10,559

Related videos on Youtube

Gnanam
Author by

Gnanam

Updated on September 18, 2022

Comments

  • Gnanam
    Gnanam over 1 year

    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:

    calljavaprogram.sh

    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.

  • Ladadadada
    Ladadadada over 12 years
    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'
  • Gnanam
    Gnanam over 12 years
    @Antitribu: If there are multiple MyJavaProgram Java process currently running, how do I pass PID dynamically to lsof command?
  • Antitribu
    Antitribu over 12 years
    @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
  • Gnanam
    Gnanam over 12 years
    @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.
  • Antitribu
    Antitribu over 12 years
    @Gnanam all good, lsof just gives you a list of open files that you can parse any way you wish.