Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:03:56+00:00 2026-06-01T03:03:56+00:00

pid = forkpty (&pty,0,0,0); if (pid == 0) { execl (/usr/bin/ssh, ssh, hostname, NULL);

  • 0
pid = forkpty (&pty,0,0,0);
if (pid == 0) {
    execl ("/usr/bin/ssh", "ssh", hostname, NULL);
    exit (0);
} else if (pid > 0) {
    ssh_pid = pid;
    ssh_pty = pty;
    if(child_ssh_success()) {
        get_user_input();
        send_user_input_to_child_ssh_and_child_forword_it_to_remote_server();
        get_remote_server_response_from_child();
        display_response_to_stdout();
    }
}

How can I tell whether ssh success or not?

How can parent know that child has successfully ssh-ed to remote server, so parent can send something to remote server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:03:58+00:00Added an answer on June 1, 2026 at 3:03 am

    Use the function waitpid in the parent ( ie inside your else if ( pid>0) condition ) to get the status of the child

    #include   <sys/wait.h>
    pid_t waitpid(pid_t pid, int *stat_loc, int options);
    

    Quoting IBM DeveloperWorks

    stat_loc

    A pointer to an integer where the wait function will return the status
    of the child process. If the waitpid function returns because a
    process has exited, the return value is equal to the pid of the
    exiting process. For this, if the value of stat_loc is not NULL,
    information is stored in the location pointed to by stat_loc. If
    status is returned from a terminated child process that returned a
    value of 0, the value stored at the location pointed to by stat_loc is
    0.
    If the return is greater than 0, you can evaluate this information using the following macros: WEXITSTATUS WIFEXITED WIFSIGNALED
    WTERMSIG.

    So if stat_loc is zero, you may assume that SSH terminated normally

    EDIT1

    If you dont want the parent to be blocked, then you need to set up a signal handler for SIGCHLD and do the same waitpid there. This time as the child has already terminated, waitpid will return immediately

    Something on these lines

    pid_t pid;
    int main ()
    {
    struct sigaction action;
    
    memset (&action, 0, sizeof(action));
    action.sa_handler = sigchld_handler;
    
    if (sigaction(SIGCHLD, &action, 0)) 
        {
        perror ("sigaction");
        return 1;
    }
    
    pid = forkpty (&pty,0,0,0);
    if (pid == 0) 
    {
        execl ("/usr/bin/ssh", "ssh", hostname, NULL);
        exit (0);
    } 
    else if (pid > 0) 
    {
        ssh_pid = pid;
        ssh_pty = pty;
    }
    }
    
    /* SIGCHLD handler. */
    static void sigchld_handler (int sig)
    {
        int chld_state;
    
        while (waitpid(pid,&child_state,options) > 0) 
        {
           if (WIFEXITED(chld_state)) 
           {
               printf("Child exited with RC=%d\n",WEXITSTATUS(chld_state));
           }
        }
    
    }
    

    Make pid global so that you can access it from sigchld_handler too. Generally ssh returns 0 for SUCCESS and 255 ( or some other positive value ) for failure, though I am not cent percent sure on this.

    EDIT2

    From our discussion, I see you want to execute commands as well on the remote server. I suggest you run ssh this

    ssh root@remoteserver.com 'ls -l'
    

    You can pass these arguments of ssh via execl() and as explained in EDIT1, you can check for its return value to verify whether everything went fine. Also, since you are doing ssh from code, you may not want to enter password manually. Here is how you do password less login

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When my program gets to this line: pid_t nPid = forkpty( &m_nMasterFD, NULL, NULL,
void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }
I get a [notice] child pid 26701 exit signal Segmentation fault (11) in apache
Considering the below code : int main() { int pid; pid=vfork(); if(pid==0) printf(child\n); else
I have this table Id PId Name Value ------------------------ 1 null foo null 2
My query is: CREATE PROC [dbo].[GetCount] @PID INT, @IA BIT=null AS BEGIN SELECT count(*)
So basically what i need is: pid = fork(); if (pid == -1) exit(1);
I need to find the PID of the current running process on a Linux
I'm currently passing the pid on the command line to the child, but is
Is there a way to get the PID from a process that is blocking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.