Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute a command line command from a C++ program

Tags:

c++

ubuntu

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

like image 817
Angel Dream Avatar asked Jan 12 '12 08:01

Angel Dream


People also ask

Can C run CMD?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler.

What command is used to execute the C program?

Answer. Step0: Install C-Program Compiler (gcc) You will need a C compiler to do this already installed, I use GCC. If you are on a Windows computer, you can run the command gcc -v to check if it's already installed.


1 Answers

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.

like image 74
unwind Avatar answered Sep 24 '22 22:09

unwind