Trigger execution of commands whenever number of file descriptors changes


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trigger execution of commands whenever number of file descriptors changes
# 1  
Old 10-19-2009
Trigger execution of commands whenever number of file descriptors changes

Hi,
I am a Unix Newbie Smilie.

I want to write a program such a way that:
Whenever number of filedescriptors opened by a process change, it should execute some commands (eg: write total number of FDs at that point of time to a file).

I dont want to poll '/proc/<pid>/fd' at regular intervals (say 1min) because number of FDs change randomly.
If number of FDs change very frequently, then it will lead to inaccurate results if polling interval is large.
If number of FDs change very rarely (couple of changes per hr) , then it is waste to keep on polling.

So is there any other way to achieve my goal?
A psuedocode, sample shell script, C, Java prog, anything is OK for me.
Kindly help me in this..

Thanks in advance..Smilie
# 2  
Old 10-19-2009
if you know ahead of time what directory tree(s) you are accessing then - if Linux - then inotify() will work just fine

inotifywatch will produce a report similar to what you are asking for as well.

You know, this sounds like you are trying to find out something and you decided that counting fd's was the answer. it may not be - what are you really doing?
# 3  
Old 10-19-2009
Thanks Jim for your suggestion. I will have a look at inotify() function and try to implement desired logic.

What I was trying to do was:
I have a program on Solaris 9 OS platform, which opens a particular file for order processing (simulation). Rate at which orders are placed is random. Higher the rate, more instances of file are opened.
Currently, FD softlimit is 256(and they say this limit cant be increased more than this value for Solaris 9).
Whenever open FDs increase more than 256, orders fail. So I want to monitor total number of file descriptors. I hope inotify() will serve the purpose (if there is Solaris equivalent for this). Smilie

Last edited by poweruser; 10-19-2009 at 11:38 AM..
# 4  
Old 10-19-2009
Solaris does not have inotify - a popen() call to fuser can do that for you. The best choice is to use the /proc directory via pfiles [pid]:
something like this --
Code:
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int openfilecnt(void)
{
    int retval=0;
    FILE *cmd=NULL;
    char tmp[256]={0x0};
    char cmdstr[PATH_MAX+1]={0x0};
    sprintf(cmdstr, "/usr/bin/pfiles %d", getpid() );
    cmd=popen(cmdstr, "r");
    if(cmd==NULL) 
       { perror("command error"); exit(1);}
    while (fgets(tmp, sizeof(tmp), cmd)!=NULL)
    {
       retval++;
    }
    pclose(cmd);
    return (retval -2)/2;  
}

Next time. mention your OS, it helps a lot.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Trigger the execution of a script on SFTP Disconnect

Hi Guys, I suspect what I'm trying to do isn't possible, but I'm hoping someone can either confirm this or point me in the right direction. We have a third-party application which transfers a collection of files to our SFTP server ( Ubuntu 12.04 with OpenSSH ) . Once the app disconnects, we... (13 Replies)
Discussion started by: jamesdrinkwater
13 Replies

3. Shell Programming and Scripting

Perl execution commands

I don't know to debug the program todaylive.pl program. plz someone let me know what are the commands I need to know to debug the perl programs to find out the error on it. (3 Replies)
Discussion started by: ramkumar15
3 Replies

4. Shell Programming and Scripting

Execution of Shell Commands

I have a question: Where would I put the Command line (of any command) so that it executes every time I log on? Where would I put it if I want it to execute every time I start a new shell? (5 Replies)
Discussion started by: Nabeel Nazir
5 Replies

5. Shell Programming and Scripting

Sequential execution of commands in ksh

I need to run few commands in a ksh script sequentially. Some of the commands are jobs submitted to the server and the consecutive commands are dependent on the completion of the jobs submitted to the server. It works if i separate the commands into different files like this #!/bin/ksh... (1 Reply)
Discussion started by: prashob123
1 Replies

6. UNIX for Advanced & Expert Users

multiple commands execution

Hi i have 3 sql scripts that need to be executed simultaneously, and independent of one another, how do i do that in Unix AIX 5.3 (1 Reply)
Discussion started by: yschd
1 Replies

7. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (6 Replies)
Discussion started by: ran16
6 Replies

8. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (0 Replies)
Discussion started by: ran16
0 Replies

9. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

10. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies
Login or Register to Ask a Question