[英]Execute Linux shell command in c++ while hiding its output
I want to execute a Linux shell command from "/sbin/" with execl or system (or another command), and hide its output. 我想使用execl或system(或其他命令)从“ / sbin /”执行Linux shell命令,并隐藏其输出。
I am using "fork" already to get a child process... 我已经在使用“ fork”来获取子进程了...
Like if I entered... 就像我进入...
service "servicename" restart
I would see the output where it says "restarting xyz [OK]". 我会在输出中看到“ restarting xyz [OK]”。 Instead, I simply want the command to be executed silently and its output discarded instead of being shown in my console application. 相反,我只是希望命令以静默方式执行并且丢弃其输出,而不是在控制台应用程序中显示该命令。
You could append this to your command: " > /dev/null 2>&1 " 您可以将其附加到您的命令:“> / dev / null 2>&1”
So your command becomes: service [servicename] restart > /dev/null 2>&1 因此,您的命令将变为:service [servicename] restart> / dev / null 2>&1
What this does is that it redirects stderr to stdout (2>&1), and redirects stdout to /dev/null ( > /dev/null) 这是将stderr重定向到stdout(2>&1),并将stdout重定向到/ dev / null(> / dev / null)
Redirect the output to /dev/null
将输出重定向到/dev/null
Eg., 例如。,
service smb restart 1> /dev/null
service smb restart 2> /dev/null
where 1
and 2
represents the stdout
and stderr
其中1
和2
代表stdout
和stderr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.