16

I have a Shell command that I'd like to run in the background and I've read that this can be done by suffixing an & to the command which causes it to run as a background process but I need some more functionality and was wondering how to go about it:

  1. I'd like the command to start and run in the background every time the system restarts.
  2. I'd like to be able to able to start and stop it as and when needed just like one can do service apache2 start.

How can I go about this? Is there a tool that allows me to run a command as a service?

I'm a little lost with this.

Thanks

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • FYI, a Unix "service" is usually called a [daemon](http://en.wikipedia.org/wiki/Daemon_%28computing%29) - knowing this may help you find information more easily. – sorpigal Nov 17 '11 at 11:54

5 Answers5

27

UNIX systems can handle as many processes as you need simultaneously (just open new shell windows if you're in a GUI), so running a process in the background is only necessary if you need to carry on using the current shell window for other things once you've run an application or process that keeps running.

To run a command called command in background mode, you'd use:

command &

This is a special character that returns you to the command prompt once the process is started. There are other special characters that do other things, more info is available here.

MattMatt
  • 2,242
  • 33
  • 30
  • 7
    The advantage of using Sorgigal's solution is that you can close the terminal/shell process, and the process will continue running. This is not the case using &, as the process remains attached to the particular shell process, and exits if the shell process does. – metasoarous Aug 13 '13 at 22:09
  • 2
    To keep `command` running after closing shell, use `nohup command &` – rem Feb 28 '16 at 17:47
22

Take a look at the daemon command, which can turn arbitrary processes into daemons. This will allow your script to act as a daemon without requiring you to do a lot of extra work. The next step is to invoke it automatically at boot. To know the correct way to do that, you'll need to provide your OS (or, for Linux, your distribution).

JavaSplice
  • 680
  • 1
  • 5
  • 10
sorpigal
  • 25,504
  • 8
  • 57
  • 75
  • Cheers Sorpigal. That helped. – Mridang Agarwalla Nov 18 '11 at 08:50
  • Hi Sorpigal. I know I've already accepted this answer but could you tell how I could get the daemon to start on startup. I'm on Ubuntu 10.4. Thanks. – Mridang Agarwalla Nov 18 '11 at 11:54
  • 1
    @MridangAgarwalla: I'm not very familiar with Ubuntu's new init system, but [try here](https://help.ubuntu.com/community/UbuntuBootupHowto) and in the Writing Services section put your daemon invocation where it says `echo "Put bash code here"` – sorpigal Nov 18 '11 at 11:58
3

Based on this article:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/

...another good way is with screen eg:

screen -d -m -s "my session name" <command to run>

from the screen manual:

-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

i.e. you can close your terminal, the process will continue running (unlike with &)

with screen you can also reattach to the session later

raratiru
  • 8,748
  • 4
  • 73
  • 113
Anentropic
  • 32,188
  • 12
  • 99
  • 147
2

Use nohup while directing the output to /dev/null

nohup command &>/dev/null &

philip mudenyo
  • 714
  • 8
  • 8
1

For advanced job control with bash, you should look into the commands jobs bg and fg.

However, it seems like you're not really interested in running the command in the background. What you want to do is launch the command at startup. The way to do this varies depending on the Unix system you use, but try to look into the rc family of files (/etc/rc.local for example on Ubuntu). They contain scripts that will be executed after the init script.

rahmu
  • 5,708
  • 9
  • 39
  • 57
  • I know that this isn't the solution for OP, but if someone swings by this answer, it is helpful to know that you can "disown" jobs and they will keep running in the background even after the shell exits. – Seamus Connor Aug 26 '15 at 17:08