0

I want to execute a Linux shell command from "/sbin/" with execl or system (or another command), and hide its output.

I am using "fork" already to get a child process...

Like if I entered...

service "servicename" restart

I would see the output where it says "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.

3
  • 1
    This is hopelessly vague. You need to say what you're doing here. This probably doesn't have anything to do with C++. You just need to read the man pages for the calls your using. You probably want to avoid ever using system on user input, because it interprets it with a shell. Perhaps you just need to be reminded to reopen stdout and stderr between fork and exec? Nov 19, 2012 at 15:02
  • I dont have a user input, i use a C++ Programme that restarts 2 specific services.
    – r4d1um
    Nov 19, 2012 at 15:29
  • possible duplicate of How to execute a shell script from C in Linux? Dec 13, 2012 at 11:02

2 Answers 2

2

You could append this to your command: " > /dev/null 2>&1 "

So your command becomes: 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)

0
1

Redirect the output to /dev/null

Eg.,

service smb restart 1> /dev/null

service smb restart 2> /dev/null

where 1 and 2 represents the stdout and stderr

2
  • with a c++ command to execute it, the output shouldn't be redirecting to stdout, should it?
    – coder543
    Nov 19, 2012 at 13:58
  • 1
    Sorry, what do you mean by "C++ command"? On all unixes, child processes inherit all fds, including stdout and stderr. More, after a fork (before exec) the process is a clone, so obviously its output goes to exactly the same place as the parent. On exec, the standard streams will be re-initialised (discarding buffers etc) but the fd for the stdio FILEs will be the same after exec. Nov 19, 2012 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.