52

I have recently completed the Wiki web development tutorial (http://golang.org/doc/articles/wiki/). I had tons of fun and I would like to experiment more with the net/http package.

However, I noticed that when I run the wiki from a console, the wiki takes over the console. If I close the console terminal or stop the process with CTRL+Z then the server stops.

How can I get the server to run in the background? I think the term for that is running in a daemon.

I'm running this on Ubuntu 12.04. Thanks for any help.

Hank
  • 3,603
  • 2
  • 17
  • 19
quakkels
  • 11,676
  • 24
  • 92
  • 149

6 Answers6

69

Simple / Usable things first

If you want a start script without much effort (i.e. dealing with the process, just having it managed by the system), you could create a systemd service. See Greg's answer for a detailled description on how to do that. Afterwards you can start the service with

systemctl start myserver

Previously I would have recommended trying xinetd or something similar for finer granuarlity regarding resource and permission management but systemd already covers that.

Using the shell

You could start your process like this:

nohup ./myexecutable &

The & tells the shell to start the command in the background, keeping it in the job list. On some shells, the job is killed if the parent shell exits using the HANGUP signal. To prevent this, you can launch your command using the nohup command, which discards the HANGUP signal.

However, this does not work, if the called process reconnects the HANGUP signal.

To be really sure, you need to remove the process from the shell's joblist. For two well known shells this can be achieved as follows:

bash:

./myexecutable &
disown <pid>

zsh:

./myexecutable &!

Killing your background job

Normally, the shell prints the PID of the process, which then can be killed using the kill command, to stop the server. If your shell does not print the PID, you can get it using

echo $!

directly after execution. This prints the PID of the forked process.

nemo
  • 55,207
  • 13
  • 135
  • 135
  • Then how would I go back and stop it, or restart? – quakkels Sep 19 '12 at 00:06
  • 5
    `&` will still hup the command when you close the terminal (depending on your shell). In bash, you need to run in it `screen`, `nohup`, or `disown` it after backgrounding it with `&` or `ctrl+Z` – lunixbochs Sep 19 '12 at 00:18
  • @lunixbochs, you're probably right to not depend on the shell's behavior on that. I'll update my answer. – nemo Sep 19 '12 at 00:31
  • Thanks for such a detailed answer! I can see how a program like upstart would be handy if you forget to record the PID when you start the background process. I'm going to experiment with both techniques and report back. – quakkels Sep 19 '12 at 01:16
  • I just tried `&!` with bash version 5.0.17(1)-release and it worked. – Orestis Kapar Nov 03 '20 at 18:26
9

You could use Supervisord to manage your process.

Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104
3on
  • 6,291
  • 3
  • 26
  • 22
6

Ubuntu? Use upstart.

Create a file in /etc/init for your job, named your-service-name.conf

start on net-device-up
exec /path/to/file --option

You can use start your-service-name, as well as: stop, restart, status

lunixbochs
  • 21,757
  • 2
  • 39
  • 47
5

This will configure your service using systemd, not a comprehensive tutorial but rather a quick jump-start of how this can be set up.

Content of your app.service file

[Unit]  
Description=deploy-webhook service
After=network.target

[Service]      
ExecStart=/usr/bin/go webhook.go    
WorkingDirectory=/etc/deploy-webhook

User=app-svc      
Group=app-svc

Restart=always    
RestartSec=10    
KillSignal=SIGINT

SyslogIdentifier=deploy-webhook-service      
PrivateTmp=true  

Environment=APP_PARAM_1=ParamA
Environment=APP_PARAM_2=ParamB

[Install]      
WantedBy=multi-user.target  

Starting the Service

sudo systemctl start deploy-webhook.service

Service Status

sudo systemctl status deploy-webhook.service

Logs

journalctl -u deploy-webhook -e
Greg
  • 1,671
  • 2
  • 15
  • 30
1

After you press ctrl+z (putting the current task to sleep) you can run the command bg in the terminal (stands for background) to let the latest task continue running in the background.

When you need to, run fg to get back to the task.

To get the same result, you can add to your command & at the end to start it in the background.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
0

To add to Greg's answer:

To run the Go App as a service you need to create a new service unit file.

However, the App needs to know where Go is installed. The easiest way to lookup that location is by running this command:

which go

which gives you an output like this:

/usr/local/go/bin/go

With this piece of information, you can create the systemd service file. Create a file named providus-app.service in the /etc/systemd/system/ using the command below:

sudo touch /etc/systemd/system/providus-app.service

Next open the newly created file:

sudo nano /etc/systemd/system/providus-app.service

Paste the following configuration into your service file:

[Unit]
Description=Providus App Service
After=network.target

[Service]
Type=forking

User=deploy
Group=deploy

ExecStart=/usr/local/go/bin/go run main.go
WorkingDirectory=/home/deploy/providus-app

Restart=always
RestartSec=10
KillSignal=SIGINT

SyslogIdentifier=providus-app-service
PrivateTmp=true

[Install]
WantedBy=multi-user.target

When you are finished, save and close the file.

Next, reload the systemd daemon so that it knows about our service file:

sudo systemctl daemon-reload

Start the Providus App service by typing:

sudo systemctl restart providus-app

Double-check that it started without errors by typing:

sudo systemctl status providus-app

And then enable the Providus App service file so that Providus App automatically starts at boot, that is, it can start on its own whenever the server restarts:

sudo systemctl enable providus-app

This creates a multi-user.target symlink in /etc/systemd/system/multi-user.target.wants/providus-app.service for the /etc/systemd/system/providus-app.service file that you created.

To check logs:

sudo journalctl -u providus-app
Promise Preston
  • 24,334
  • 12
  • 145
  • 143