P
P
pka064552015-11-13 21:33:20
linux
pka06455, 2015-11-13 21:33:20

How do I call a bash script from C++ in a daemon?

Hello!
I created a daemon according to the article - www.lissyara.su/articles/freebsd/coding/socket_server
This example works very well on the console. But as soon as I decided to change the functions in it, everything started working only once. Now more.
I need to call a bash script (or rather /etc/init.d/service stop) when I enter a command on the console. I substituted the system("/etc/init.d/service stop") function in the if(cmd == "info") condition as an example. The first call everything works fine, but the rest don't start. More precisely, if you set the output of the text before the system function, then it works, but the system function itself is gone from the second time. How can I replace this function or what am I doing wrong?
I add:
the second execution of system returns an empty string

char *cmd = "/etc/init.d/service stop";
          char buf[BUFSIZ];
          FILE *ptr;

          if ((ptr = popen(cmd, "r")) != NULL)
                  while (fgets(buf, BUFSIZ, ptr) != NULL)
                          (void) printf("%s", buf);
                  (void) pclose(ptr);

And the second execution of this code generally hangs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Tsilyurik, 2015-11-13
@Olej

I substituted in the example in the condition if (cmd == "info")

I assume that complete garbage is written here:
- cmd, "info" are pointers to a string, char* ...
- content cannot be compared by comparing pointers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question