Shell – How to start a program in the background

background-processshellsudo

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

Best Answer

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).

Related Question