0

I'm trying to create a background program that has main config file which contains another config file locations and execute each of them. For example: if main_config.conf:

/home/conf1
/home/conf2

i want to execute ./background_pro /home/conf1 and ./background_pro /home/conf2 in background. How do i do that ? I'm using below structure.Thank you

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

int main(void) {

        /* Our process ID and Session ID */
        pid_t pid, sid;

        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

        /* Open any logs here */        

        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }



        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }

        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        /* Daemon-specific initialization goes here */

        /* The Big Loop */
        while (1) {
           /* Do some task here ... */

           sleep(30); /* wait 30 seconds */
        }
   exit(EXIT_SUCCESS);
}
Odko
  • 141
  • 1
  • 8
  • What is your question, I mean what exactly troubles you? – Iharob Al Asimi May 25 '15 at 22:48
  • After closing the standard descriptors you should probably open them on `/dev/null`, since some library functions may expect them to be open. – Barmar May 25 '15 at 22:48
  • Other than that, your structure seems OK. – Barmar May 25 '15 at 22:49
  • You need to change the arguments to `main` to be `int argc, char*argv[]` so you can read the filename argument with the config file. – Barmar May 25 '15 at 22:50
  • @iharob how to execute program multiple times with different arguments(argv) from daemon ? – Odko May 25 '15 at 22:51
  • @akiD I am sorry it's still unclear, why would you execute it multiple times with different arguments? does it have to fork and execute itself? what are you trying to do? – Iharob Al Asimi May 25 '15 at 22:54
  • @Barmar Thanks, how do i execute multiple times with arguments from daemon ? – Odko May 25 '15 at 22:56
  • 1
    I don't understand your problem. You just type the command from the shell prompt multiple times, and give it different command line arguments. The "Daemon-specific initialization" code should open the file and read the configuration options from there. – Barmar May 25 '15 at 23:01
  • Do you not know how to read from a file? – Barmar May 25 '15 at 23:02
  • Sorry about my english :) I want to make a child process for each arguments :) – Odko May 25 '15 at 23:03
  • @Barmar can i make that program execute once command from shell prompt and make that program execute multiple times ?. if i execute `./daemon` it executes `./program conf1` `./program conf2` :) – Odko May 25 '15 at 23:08
  • Aren't you going to create zombie processes? – Fiddling Bits May 25 '15 at 23:14
  • 2
    `./daemon` can be a shell script that executes those two commands. – Barmar May 25 '15 at 23:17
  • @FiddlingBits They're not zombies because the parent exits. – Barmar May 25 '15 at 23:18
  • Thanks for your time :) Looks like this was a stupid question :) I thought i could make c program that makes child processes and executes itself multiple times other than shell script :). – Odko May 25 '15 at 23:21
  • @Barmar how do i run these simultaneously ? – Odko May 26 '15 at 06:09
  • Just run them one after the other. They'll run simultaneously because each of them puts itself in the background. – Barmar May 26 '15 at 06:11
  • I'm assuming that `./program` is the program you showed in the question. – Barmar May 26 '15 at 06:12

2 Answers2

1

Just append an '&' at the rear of shell command, then the command will run in background. I think, this may be what you want: daemon.bash

#!bash

# to stop a sub process
if [ "x$1" = "x-stop" ]; then
    if [ "x$2" = "xsub1" ]; then
        kill `cat sub1.pid`
    elif [ "x$2" = "xsub2" ]; then
        kill `cat sub2.pid`
    fi
    exit
fi

# to start sub processes
conf1=`head -1 main_config.conf`
conf2=`head -2 main_config.conf | tail -1`
nohup ./background_pro $conf1 &
pid1=$!
echo "$pid1" > sub1.pid
nohup ./background_pro $conf2 &
pid2=$!
echo "$pid2" > sub2.pid

Run the demon:

nohup ./daemon.bash &

The nohup will keep the program running even you turn off the terminal.

Stop sub process 1:

./daemon.bash -stop sub1
Hank Chang
  • 171
  • 1
  • 8
0

A background program might be what linux users call a "Daemon" program. Please look here on Creating a daemon in Linux where you can find a plethora of information on how to begin writing your own Daemon. For parsing command line arguments, i recommend reading the GNU c library manual for getopt().

Community
  • 1
  • 1
JD_GRINDER
  • 334
  • 2
  • 6