28

How can I execute the command line "asterisk -rx "reload"" in c++? Please help. I need an example. I am working on ubuntu server and I want to execute this command line from a user (inside a webservice).

Need help Appreciate

David Yaw
  • 25,725
  • 4
  • 59
  • 90
Angel Dream
  • 331
  • 1
  • 4
  • 12
  • 1
    Did you really mean C++/CLI, of did you mis-interpret the tag [c++-cli] to mean "C++ command line interface"? Is there actually a C++/CLI implementation for Linux? – celtschk Jan 12 '12 at 09:07

2 Answers2

29

Sounds like a trivial use-case for the system() function:

system("asterisk -rx reload");

If you need very fine-grained control of the child process there are better ways, but this is simple to get going.

This call starts a shell (such as bash) to run the command, which is why I removed the quotes around reload; they're pointless for a single word and will be removed by the shell and never seen by the started program, anyway.

unwind
  • 364,555
  • 61
  • 449
  • 578
  • This is good, an alternative method could be fork/exec to (requires more effort but more powerful overall) – dreamlax Jan 12 '12 at 09:03
  • i tested it byt is not wkiring – Angel Dream Jan 12 '12 at 10:36
  • i have a web service on a server and i trying to call this method from the user but the asterisk is not reloading. why? need help plz – Angel Dream Jan 12 '12 at 10:37
  • @AngelDream: The program that calls the `system` function must have the correct privileges to execute the `asterisk` program. For example, if your program is running as a normal user, but `asterisk` requires a root user, then the `system` call will not work because the your program is not allowed to execute `asterisk`. – dreamlax Jan 12 '12 at 20:39
5

system("asterisk -rx \"reload\"") would probably work, if you don't need standard output or error from the process.

If you need results from the process, here is an example of using C's popen(), or you could look at Boost.Process for a C++ approach.

Community
  • 1
  • 1
Alex Reynolds
  • 91,635
  • 50
  • 223
  • 320