简体   繁体   English

如何从C ++程序执行命令行命令

[英]How can I execute a command line command from a C++ program

How can I execute the command line "asterisk -rx "reload"" in c++? 如何在c ++中执行命令行“asterisk -rx”reload“”? 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). 我正在使用ubuntu服务器,我想从用户(在webservice内)执行此命令行。

Need help Appreciate 需要帮助欣赏

Sounds like a trivial use-case for the system() function: 听起来像system()函数的一个简单的用例:

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 ; 这个调用启动一个shell(比如bash)来运行命令,这就是为什么我在reload周围删除了引号; they're pointless for a single word and will be removed by the shell and never seen by the started program, anyway. 无论如何,它们对于一个单词都是毫无意义的,并且将被shell删除,并且从未被启动过的程序看到过。

system("asterisk -rx \\"reload\\"") would probably work, if you don't need standard output or error from the process. 如果您不需要标准输出或过程中的错误, system("asterisk -rx \\"reload\\"")可能会起作用。

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. 如果你需要这个过程的结果, 这里有一个使用C的popen()的例子,或者你可以看看Boost.Process的C ++方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM