Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    execlp, ssh and fork

    I have a program where i ssh into a server and gets data. Here is the code... I fork it and the child executes the query and the parent waits for the child for a predetermined amount of time (in function timeout) and then kills the child. I did that part because sometimes, i am not exactly sure why, but the ssh connection stops and doesnot exit. That is there is a "ssh -oConnectTimeout=60 blah blah" in the processes list for a long and the timeout function doesnt seem to work. What am i doing wrong here? The last time this problem occured, there was an ssh in process list for 5 days and still it didnot timeout and the program had stopped because it was waiting for the child. There are those extra wait() functions because previously i was getting a lot of defunct processes a.k.a zombies. So i took the easy way out..

    c = fork();
    if(c==0) {

    if (dup2(fd[WRITE],STDOUT_FILENO) != -1)
    execlp("ssh", "ssh -oConnectTimeout=60", serverDetails.c_str(), NULL);

    if(timeout(c) == 1){
    waitpid(c, &exitStatus, WNOHANG);
    return 0;
    This is the timeout function.

    int timeout(int childPID)
    int times = 0, max_times = 10, status, rc;
    while (times < max_times){
    rc = waitpid(childPID, &status, WNOHANG);
    if(rc < 0){
    if(WIFEXITED(status) || WIFSIGNALED(status)){
    /* child exits */

    if (times >= max_times){
    return 1;
    else return 0;

  2. #2
    It looks ok but what I can't tell is what is the path is execution. You should put in some debug statements or use a debugger to see what direction the code is taking. If you are getting zombies then your parent program is exiting before the child is, but how could that be with where you have the waits?
    So, what is the path the code is taking to have that happen?
    Does the timeout function get called and do you see it break out of the loop. If timeout is true, do you see the call to kill? Should the SIGTERM be SIGKILL? Do it get passed kill?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts