Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a command as a background process/service

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

like image 846
Mridang Agarwalla Avatar asked Nov 17 '11 09:11

Mridang Agarwalla


People also ask

How do I run a command in the background process?

If you want to run additional commands while a previous command runs, you can run a command in the background. If you know you want to run a command in the background, type an ampersand (&) after the command as shown in the following example. The number that follows is the process id.

Which of the following commands will run as a background process?

The bg command is used to resume a background process. It can be used with or without a job number. If you use it without a job number the default job is brought to the foreground. The process still runs in the background.

What does it mean to run a process in the background?

A background process is a computer process that runs behind the scenes (i.e., in the background) and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification.


1 Answers

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.

like image 104
MattMatt Avatar answered Oct 07 '22 14:10

MattMatt