Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The program Boblight does not run in background. There is no noticeable difference between executing

sudo boblightd

and

sudo boblightd& 

How can I solve this problem that the console will not block further inputs?

pi@raspberrypi ~/boblight/boblightd-for-raspberry-master $ sudo boblightd
Boblightd 2.0 (optimized version for raspberry) (c) 2013 Speedy1985 and Heven)
(InitLog)                       start of log /root/.boblight/boblightd.log
(PrintFlags)                    starting boblightd
(CConfig::LoadConfigFromFile)   opening /etc/boblight.conf
(CConfig::CheckConfig)          checking config lines
(CConfig::CheckConfig)          config lines valid
(CConfig::BuildConfig)          building config
(CConfig::BuildConfig)          built config successfully
(main)                          starting devices
(CClientsHandler::Process)      opening listening TcpSocket on *:19333
(CDevice::Process)              ambilight: starting with output "/dev/spidev0.0"
(CDevice::Process)              ambilight: setting up
(CDevice::Process)              ambilight: setup succeeded


pi@raspberrypi ~/boblight/boblightd-for-raspberry-master $ sudo boblightd&
[1] 2289
pi@raspberrypi ~/boblight/boblightd-for-raspberry-master $
Boblightd 2.0 (optimized version for raspberry) (c) 2013 Speedy1985 and Heven)
(InitLog)                       start of log /root/.boblight/boblightd.log
(PrintFlags)                    starting boblightd
(CConfig::LoadConfigFromFile)   opening /etc/boblight.conf
(CConfig::CheckConfig)          checking config lines
(CConfig::CheckConfig)          config lines valid
(CConfig::BuildConfig)          building config
(CConfig::BuildConfig)          built config successfully
(main)                          starting devices
(CClientsHandler::Process)      opening listening TcpSocket on *:19333
(CDevice::Process)              ambilight: starting with output "/dev/spidev0.0"
(CDevice::Process)              ambilight: setting up
(CDevice::Process)              ambilight: setup succeeded
share|improve this question

migrated from stackoverflow.com Nov 14 '13 at 14:12

This question came from our site for professional and enthusiast programmers.

7  
Say: sudo boblightd > /dev/null 2>&1 & –  devnull Nov 14 '13 at 7:57
    
Thanks. I will check this. –  user2534685 Nov 14 '13 at 8:00

5 Answers 5

The [1] 2289 after your background command shows it worked, and was indeed put into the background.

But the output of your command will still go to the terminal, unless you redirect. Here is the comprehensive way to do that:

sudo boblightd >std.txt 2>err.txt &

If you want both stdout and stderr to go to the same file:

sudo boblightd >std.txt 2>&1 &

And, of course, if you don't care about the output of either or both streams, you can send to /dev/null instead of a filename.

sudo boblightd >/dev/null 2>err.txt &

(that example throws away standard output, but keeps stderr, just in case something goes wrong.)


UPDATE

The above is based on no knowledge of what boblightd is. I see in another answer it has a daemon mode, so that should be used instead in this case.

BTW, the above assumes sudo will not prompt for a password, and that you will not close the terminal window. For the former, I personally normally use sudo bash then, would type boblightd >std.txt 2>err.txt &. Another way is to do sudo ls or some harmless command to make sure the access gets cached.

For the latter, nohup is the magic command to make sure it stays running even after you Leave The Building. It would go after the sudo and before the actual command. E.g. sudo nohup boblightd >std.txt 2>err.txt &. Or sudo bash then nohup boblightd >std.txt 2>err.txt &, then exit (to leave the root shell).

share|improve this answer
    
It is also important to note that if sudo tries to prompt for the password, it'll fail because background processes cannot read STDIN. –  Patrick Nov 15 '13 at 1:33
    
Thanks @Patrick. I had just answered on the immediate problem in the question, but as this is getting some attention I have updated with mention of password prompting and nohup. –  Darren Cook Nov 15 '13 at 2:42

You'll have to redirect stdout and stderr to something else than their defaults to hide them. @devnull's comment shows how to do this. That's a first step.

If you simply use & to detach your program, it will be killed automaticaly when you logout. That's probably not what you want. You'll have to use the nohup command to prevent this:

nohup sudo boblightd > boblight.out 2> boblight.err < /dev/null &

Note that sudo will probably ask for a password: it won't get any, and you won't notice it's asking you for one, as every output/input is redirected. You have different solutions to achieve this anyway:

  • Run a sudo command before, sudo will save your password. That's the quick'n'dirty way.
  • Remove the &, give sudo the password, then send the process to background with CTRL + Z.
  • Can also configure sudo to not ask the password for this specific user and executable couple. Be aware that it could be a possible security breach, …
share|improve this answer
    
Is it better to have the nohup before of after the sudo? How does sudo get it's password when you run it under nohup? –  Aaron Digulla Nov 14 '13 at 8:21
1  
Well, that's a good point: it won't. You have multiple solutions here: run a sudo command before, sudo will save your password. That's the quick'n'dirty way. Another one would be to remove the &, give sudo the password, then send the process to background with ctrl + z. You can also configure sudo to not ask the password for this specific user and executable couple. –  Kernald Nov 14 '13 at 8:38
    
(I added this to the answer.) –  Kernald Nov 14 '13 at 8:55
    
I normally do sudo bash, then run the nohup command from inside the new shell. –  Darren Cook Nov 14 '13 at 12:22
    
Actually it is incorrect that if you only use & do background the process that it'll be killed on logout. Only if bash is killed with a SIGHUP will it then SIGHUP your program. But logging out does not cause a SIGHUP. If you type logout or use CTRL+D, bash will exit and leave the process running. SIGHUP is sent when you close the terminal emulator in a GUI. –  Patrick Nov 15 '13 at 1:30

I think you can use nohup command like this:

nohup sudo boblightd &

this will put the output of your command to nohup.out file in current directory.

Also you can use screen command like this : first create a screen :

screen -S your-sreen-name

then run your command:

sudo boblightd

To save the screen and return to the terminal, type Ctrl+AD (Ctrl+A is the hint for screen that you want to do something and D then "d"etaches from the session without stopping it).

restore your screen :

screen -d -r your-screen-name
share|improve this answer

Boblightd

In the case of boblightd the output sent to stderr is already captured in it's log file (defaults to ~/.boblight/boblightd.log), so this doesn't need to be captured and can be discarded. Also, boblight doesn't fork by default but can be made to do by include the -f in the command.

I suggest you try the following:

sudo boblightd -f >/dev/null 2>/dev/null

(details from the project documentation)

More generally

Processes started from the shell will terminate when the shell exits. As others have pointed out using Ctrl-Z while running a process in the foreground returns control to the shell. However, the process is placed in a stopped state at this point. The bg command will be needed to get the process running again but stay in background. It requires either a process id or the job number (as usual job numbers are prefixed with a %), so using your second example you would issue either

bg %1

or

bg 2289

The process will now be running in the background but is still attached to the shell. The link to the shell can be severed using the disown command, which saves the confusion with nohup/sudo. As with bg, disown requires only the process id or job number

e.g.

disown 2289

You can now exit the shell safely as if you had run the process with the nohup command

share|improve this answer

Since the answer is pretty much laid out in comment already. I felt like it would be good to post a little bit of explanation on it as an actual answer.

So the way to go is: sudo boblightd > /dev/null 2>&1 &

There are 3 important parts here. Most important is & at the end of line. It makes the shell not wait for command to end before giving back control. It also takes out the standard input from keyboard. It puts the job into background.

But this still would leave the output back into console. To prevent this there is > symbol which redirects the standard output (to /dev/null in this case).

At this point you can still get output from called command get to screen. This would be the standard error. So lastly there is 2>&1 magic. This redirects output from the standard error stream to the standard output.

2 and 1 come from standard file descriptors. 0 would be stdin, 1 - stdout, 2 - stderr.

You could redirect output to a file if you need it.

There are command designed to interact with jobs in back ground. jobs and fg.

You could also play with more advanced solutions such as screen command if needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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