Find the answer to your Linux question:
Results 1 to 10 of 10
Hi, I wanted to know some method to run an executable from a process i.e. run one process using another.suppose there are 2 process which i need to monitor on ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Location
    Bangalore,India
    Posts
    9

    Executing number of processes from a process


    Hi,
    I wanted to know some method to run an executable from a process i.e. run one process using another.suppose there are 2 process which i need to monitor on 1.ProcessA and 2.ProcessB.
    I need to check in another program called Main,whether ProcessA is running or not if it is running then move forward to check for ProcessB else if ProcessA is not running then Execute ProcessA and then Move forward to ProcessB.
    I searched google and i found exec(),system() etc, but i dont want to use exec().and using system() my program waits for ProcessA to complete.so in need Main to start a process and move forward to check for another process w/o waiting for a process to be completed.
    Or can i schedule a process by myself to run in my code.how to do this?
    One more question:How to make a process run when we restart our machine?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    I need to check in another program called Main,whether ProcessA is running or not
    if it is running then move forward to check for ProcessB
    else if ProcessA is not running then Execute ProcessA and then Move forward to ProcessB.
    So you "move forward" whether ProcessA runs or not.
    Which would mean, that ProcessA and ProcessB are independent.
    If so,
    - write a init script for each. This will start them at boot time
    - monitor them individually. How to do that depends on the processes. Maybe via pid, or check ports, or actually run tests with expected results.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Location
    Bangalore,India
    Posts
    9
    Hey Thanks for your reply.Actually I can check them using there pid in my Main program but i dont know how to start them.i dont know how to write a script to start them.And yes these two processes are independent.Can u post a script code to start a process called "ProcessA"?I will be much helpfull.And can i run a script using a c program?
    Last edited by ankitasingh; 08-29-2012 at 11:32 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    Well, in the simplest case:
    Code:
    <PATH>/ProcessA
    This implies, that ProcessA detaches properly.

    As for init scripts, this depends on your distribution.
    For RedHat6, it looks like this:
    https://access.redhat.com/knowledge/..._Examples.html
    You must always face the curtain with a bow.

  6. #5
    Just Joined!
    Join Date
    Aug 2012
    Location
    Bangalore,India
    Posts
    9
    I am using centos. Wel i got some scripts under /etc/init.d.But can i run them using a c code just for the task of restarting the processes?

  7. #6
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    Yes, most probably with exec(), as you already know.
    However, as you intend to restart these processes from your main application you would also need to develop and implement some monitoring, cleanup and starting code.

    This is possible and depending on the usecase maybe even neccessary.
    However, if processA and processB are independent, then maybe a different approach is to "outsource" the starting and monitoring to initscripts and a monitoring solution.

    As an example:
    apache and mysql are two processes.
    They do depend on each other for about any dynamic website.
    But neither mysql is starting and monitoring apache nor vice versa.
    They both have their respective initscripts and are (hopefully) monitored by e.g. nagios or xymon.


    But your usecase might be different.
    Are ProcessA and ProcessB quite similar in purpose, maybe sharing data/config, and you need a way to govern them?
    Then have a look at gearman.
    You must always face the curtain with a bow.

  8. #7
    Just Joined!
    Join Date
    Aug 2012
    Location
    Bangalore,India
    Posts
    9
    Basically i am writing a server code.and i am having 10 processes which are listening on different ports.server code communicates with these processes and these processes are connected to client.So a program is needed to start a process if it is not running as these process are important to be running all the time.

  9. #8
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    Hmm, I would probably stick to the init script approach.
    10 independent init scripts, one for each process.
    Init scripts are the standard way of handling daemon startup/stop.

    As for the availability part:
    Monitoring is a definite requirement.
    If a process crashes, then chances are that a simple restart might not fix it.
    So you need to graph and log these processes and inform someone in case of a downtime.

    If you go for high availability:
    One way would be to have multiple machines with these 10 processes each.
    Then a pair of redundant loadbalancers in front to distribute requests to only healthy nodes.

    Of course, multiple machines then have consequence on data integrity and synchronisation.
    You must always face the curtain with a bow.

  10. #9
    Just Joined!
    Join Date
    Dec 2009
    Location
    California
    Posts
    98
    Ok, not that I mean to be over-simplistic, but couldn't you just do something like this:
    Code:
    #!/bin/sh
    while true
    do
         ./processA
    done
    Do that for each process and have an init script run that with nohup in the background.

    If processA fails, this will restart it immediately. No need to check to see if it is running.

    If you really want to use a C program, then the method for starting processA is like this:
    Code:
     switch (fork())
            {
            case -1:
                    printf("Can't create new process\n");
                    break;
            case 0: /* child */
                    execvp("processA", argv);
                    printf("Can't execute processA\n");
                    exit(1);
                    break;
            default:        /* parent */
                    if (wait(&status) == -1)
                            syserr("wait");
                    printf("Status=0x%04x\n", status);
            }
    argv is a null terminated array of arguments to processA

  11. #10
    Linux Newbie
    Join Date
    Nov 2009
    Posts
    238
    Look up the manual for init. It (used) to have the ability to "respawn" a process started by itself.

Posting Permissions

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