Thread: Send linux ps command output

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Send linux ps command output

    Hi dears,
    I have a client,server c program in linux,
    Can you help me, for send linux ps command via socket from client to server for save to server?

    Very thanks for your helpful answers,

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    703
    The client needs to fork, dup the server socket fd to stdout, and exec the ps command. Here's an example that outputs to a file.
    Code:
    #define _BSD_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main() {
        pid_t pid = fork();
        if (pid == (pid_t)-1) {
            perror("Error: fork");
            exit(EXIT_FAILURE);
        }
    
        if (pid == 0) {  // child
    
            // you don't need to open a file
            int fd = open("ps_out.txt", O_WRONLY|O_CREAT, 0666);
            if (fd == -1) {
                perror("Error: open");
                exit(EXIT_FAILURE);
            }
    
            close(STDOUT_FILENO);
            dup(fd);  // you would dup the server socket fd here
    
            execlp("ps", "ps", "-A", (const char*)NULL);
    
            perror("Error: execl");
            exit(EXIT_FAILURE);
        }
    
        // parent
        wait(NULL);
    
        printf("PARENT EXITING\n");
    
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-15-2017, 07:24 AM
  2. Send counter output to LCD
    By gregfox in forum C Programming
    Replies: 9
    Last Post: 01-16-2016, 07:44 PM
  3. 15.000 euros to send me to command line?
    By nonlinearly in forum Windows Programming
    Replies: 14
    Last Post: 02-29-2012, 11:29 AM
  4. Replies: 8
    Last Post: 10-24-2005, 11:26 AM
  5. changing from name in net send command
    By cfrost in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-04-2004, 07:42 PM

Tags for this Thread