szpal
szpal

Reputation: 647

sometimes execute external command is failed (sooner or later)

I wrote a server app, let we call simplicity 'server', which is work in the background. Here is the core of the server:

int main( int argc, char **argv ) {
  // define some variables..
  // fork()..
  // connect to MySQL
  // etc...

  while (1) {
    myfunc_1();
    myfunc_2();
    myfunc_3();
    myfunc_4();
    myfunc_5();
    myfunc_6();
    myfunc_7();
    ...
    syslog( LOG_NOTICE, "Sleeping..." );
    sleep( 90 );
  }

  exit (EXIT_SUCCESS);
}

The problem is in the myfunc_3() and myfunc_5().

Core of both function call a external system command: myfunc_3() -> nfdump, myfunc_4() -> docsis.

Here is some relevant code:

int myfunc_3( void ) {
  // define some variables
  int ret;
  char nfdump_cmd[1024];

  // I assemble the 'nfdump_cmd' command-string with sprintf..

  ret = system( nfdump_cmd );
  if ( ret == 0 ) {
    syslog( LOG_NOTICE, "nfdump success." );
  }
  else {
    syslog( LOG_ERR, "nfdump failed!" );
    return (-1);
  }

  return (0);
}

The myfunc_5() is very similar, he call the docsis program (Docsis encoder), when we need create a modem/mta config file.

The issue: The 'server' run in the background, and doing its job, but only 2-3 weeks!

Sooner or later the myfunc_3() OR the myfunc_5() cannot execute their external commands successful, it report that return code is -1. (if myfunc_3() unsuccessful, then the myfunc_5() is also unsuccesful, this is 100%.)

In this case, does not need to do anything else, just restart the server, and all works again! (until 2-3 weeks...)

I tried replace the 'system' command with 'popen' method, but the result is same :(

My question is: How could this nasty error(s) to debug, and solve?

Upvotes: 1

Views: 82

Answers (1)

Jeegar Patel
Jeegar Patel

Reputation: 27230

After some days your server gets failed so i guess below can be reasons.

1) Make sure how many fopen() or open() happen that all are getting fclose() or close(). In linux any process can have only limited number of opened descriptor.

2) Make sure Heap and other resources whose are getting allocated are getting freed after use.

3) Make sure any function is not aggressively using stack and causes process to terminated.

Upvotes: 1

Related Questions