Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
I substituted in the example in the condition if (cmd == "info")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question