注册为 Windows 服务运行

前提要求

想要使 Gogs 通过 Windows 服务的方式运行,必须满足以下两个条件:

miniwinsvcminiwinsvc
[repository] ROOT
C:\Gogs\custom\conf\app.ini
  1. RUN_USER = COMPUTERNAME$
COMPUTERNAMEecho %COMPUTERNAME%USER-PCRUN_USER = USER-PC$
  1. [server]
  2. DOMAIN = gogs
  3. PROTOCOL = http
  4. HTTP_ADDR = 127.0.1.1
  5. HTTP_PORT = 80
  6. OFFLINE_MODE = true
  7. ROOT_URL = http://gogs/
127.0.0.2 - 127.254.254.254
C:\Windows\System32\drivers\etc\hosts
  1. # Gogs local HTTPd
  2. 127.0.1.1 gogs
gogs/

Use Builtin Functionality

cmd.exe
  1. C:\> sc create gogs start= auto binPath= "\"C:\gogs\gogs.exe\" web --config \"C:\gogs\custom\conf\app.ini\""
=

To start the service run following command:

  1. C:\> net start gogs

You should see following output:

  1. The gogs service is starting.
  2. The gogs service was started successfully.

Use NSSM

%PATH%

Open a command line as administrator and do following command to configure Gogs as a service:

  1. C:\>nssm install gogs

“NSSM service installer” will appear. Configure it as follows:

Application tab:

C:\gogs\gogs.exeC:\gogsweb

Details tab:

GogsA painless self-hosted Git service.Automatic (Delayed Start)

Note that we’ve chosen delayed start, so that the service will not impact the early boot time. Gogs will start two minutes after the non-delayed services.

I/O tab:

C:\gogs\log\gogs-nssm.txtC:\gogs\log\gogs-nssm.txt

That will capture all text output that you would normally receive from Gogs on the command line console, and log it to that file instead.

File rotation tab:

Rotate files1000000 bytes

Environment tab:

PATH=%PATH%;C:\gogs;C:\Program Files (x86)\Git\bin
gogs.exegit.exe

Click “Install service”, and you should be confirmed that it succeeded.

If it failed, refer back to the command line console you started, for the error message. When it succeeds, go to command line and do:

  1. nssm start gogs

You should see:

  1. gogs: START: The operation completed successfully.
C:\gogs\log\gogs-nssm.txt
  1. timestamp [I] Run Mode: Production
  2. timestamp [I] Listen: http://127.0.1.1:80
http://gogs/

Gogs is running as a service, and will not need to be run manually, unless something goes wrong. NSSM will attempt to restart the service for you, if the Gogs server crashes.

app.ini
  1. nssm restart gogs
custom\conf\app.ini

An example of the logging and error handling in action:

  1. [log]
  2. ROOT_PATH = C:\gogs\log
C:\gogs\log\gogs-nssm.txt
  1. timestamp [T] Custom path: C:/gogs/custom
  2. timestamp [T] Log path: C:\gogs\log
  3. timestamp [I] Gogs x.y.z
  4. timestamp [log.go:294 Error()] [E] Fail to set logger(file): invalid character 'g' in string escape code
C:\custom\conf\app.ini
  1. ROOT_PATH = C:/gogs/log
nssm
  1. C:\>nssm restart gogs
  2. gogs: STOP: The operation completed successfully.
  3. gogs: Unexpected status SERVICE_PAUSED in response to START control.
  4. C:\>nssm start gogs
  5. gogs: START: An instance of the service is already running.
  6. C:\>nssm stop gogs
  7. gogs: STOP: The operation completed successfully.
  8. C:\>nssm start gogs
  9. gogs: Unexpected status SERVICE_PAUSED in response to START control.
  10. C:\>nssm restart gogs
  11. gogs: STOP: The operation completed successfully.
  12. gogs: START: The operation completed successfully.