# Run Julia server as Windows Service to work after user log out

**URL:** https://discourse.julialang.org/t/run-julia-server-as-windows-service-to-work-after-user-log-out/37261
**Category:** General Usage
**Tags:** question, windows, services
**Created:** [April 9, 2020, 3:04am UTC](https://discourse.julialang.org/t/run-julia-server-as-windows-service-to-work-after-user-log-out/37261 "2020-04-09T03:04:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)
#### Post date: [April 9, 2020, 3:04am UTC](https://discourse.julialang.org/t/run-julia-server-as-windows-service-to-work-after-user-log-out/37261/1 "2020-04-09T03:04:47Z")

</div>

I’m trying to make Julia server working on a Windows machine without being closed when a user log out.

So, I’ve tried to wrap a simple Julia HTTP server script (and WebSocket server script) into a service on Windows 10 x64 platform – with no success yet.

What I am doing wrong?

**Prerequisites:**

- Create Julia script file with a simple HTTP server (HTTP.jl package should be added): _C:\test\_julia\_server\http\_server.jl_

```julia
using HTTP
import Sockets
using Dates
open("C:\\test_julia_server\\log.txt", "a") do io
    println(io, "$(now()) Julia http_server script enter")
end

function handle(reg::HTTP.Request)
    "<body>Hello from Julia!</body>" |> HTTP.Response
end

# define REST endpoints to dispatch to "service" functions
router = HTTP.Router()
HTTP.@register(router, "GET", "", handle)
localIP = Sockets.localhost
port = 8080
@sync HTTP.serve(router, localIP, port) # @sync to wait for completion
# in browser enter: http://127.0.0.1:8080/

```

- Another option – create Julia script file with a simple WebSocket server (WebSockets.jl package should be added): _C:\test\_julia\_server\ws\_server.jl_

```julia
using Dates
open("C:\\test_julia_server\\log.txt", "a") do io
println(io, "$(now()) Julia ws_server script enter")
end

using WebSockets
serverWS = WebSockets.ServerWS((req) -> WebSockets.Response(200),
(ws_server) -> (writeguarded(ws_server, "Hello"); readguarded(ws_server)))

WebSockets.with_logger(WebSocketLogger()) do
    WebSockets.serve(serverWS, port = 8000)
end

```

- Create batch file _C:\test\_julia\_server\run\_server.bat_

```plaintext
    @echo off
    @echo Batch: enter>> C:\test_julia_server\log.txt
    cd C:\Julia\1.4.0_x64\bin\
    @echo Batch: dir changed>> C:\test_julia_server\log.txt
    julia.exe C:\test_julia_server\http_server.jl
    @echo Batch: exit>> C:\test_julia_server\log.txt

```

Now, let’s try to run it as a service on Windows.

* * *

**1. Using built-in tool SC**

- Run cmd as admin and type  
`sc create julia_sc start= demand displayname= "julia_sc" binpath= C:\test_julia_server\run_server.bat`

- Try to start a service  
`sc start julia_sc`

`Error 1053: The service did not respond to the start or control request in a timely fashion.`

- Failed, so delete the service:  
`sc delete julia_sc`

- Log file:

```julia
Batch: enter
Batch: dir changed
Batch: exit

```

**Looks like it cannot even execute Julia…**

* * *

**2. Using NSSM**

> <https://stackoverflow.com/questions/3582108/create-windows-service-from-executable>

- Run cmd as admin from the folder with nssm.exe  
`nssm install julia_server_nssm`

- Fill in the form, several tries:

#1: Call julia executable with script file as an argument  
 ![](https://global.discourse-cdn.com/julialang/original/3X/6/8/68d49f4024b53ea982109575a3bd6ac47229b89f.png)  
#2 Call batch file _C:\test\_julia\_server\run\_server.bat_  
 ![](https://global.discourse-cdn.com/julialang/original/3X/8/8/88d6fd89c5874f14ad03387106fd274d2251befe.png)

- Trying to start the server, it returns error  
`nssm start julia_server_nssm`

`julia_server: Unexpected status SERVICE_PAUSED in response to START control.`

- Or start from Services window and get:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/e/c/ec261b9d2a1468acc13e75c62d1ff2c6ff7ab40a.png)

- Failed, so stop and delete the service  
`nssm stop julia_server_nssm`  
`nssm remove julia_server_nssm`

- Check log file:

#1:

```julia
2020-04-09T04:16:26.281 Julia http_server script enter
2020-04-09T04:16:31.599 Julia http_server script enter
2020-04-09T04:16:40.933 Julia http_server script enter

```

#2:

```julia
Batch: dir changed
3:53:40 Julia http_server script enter
Batch: exit
Batch: enter
Batch: dir changed
3:53:43 Julia http_server script enter
Batch: exit

```

**Looks like it actually runs Julia script, but fails to wait inside HTTP.serve and repeats… The same with websocket server.**

* * *

**3. Using RunAsAlive**

> <https://stackoverflow.com/questions/415409/run-batch-file-as-a-windows-service>

- Run from console:

`RunAsService install "julia_asalive" "C:\Julia\1.4.0_x64\bin\julia.exe" "C:\test_julia_server\http_server.jl"`  
`net start "julia_asalive"`

- Log file output - server it starts only once:  
`2020-04-09T05:00:13.561 Julia script enter!`

- Try to access it from browser:  
`http://127.0.0.1:8080/` - Connection refused, so it is not working.

- Try to stop it:  
`net stop "julia_asalive"`

`The service could not be controlled in its present state.`

**So, it just stuck dead in services list.**

* * *

**4. Using SrvStart**

> **[GitHub - rozanski/srvstart: This repository contains the legacy code I wrote...](https://github.com/rozanski/srvstart)**
>
> This repository contains the legacy code I wrote for SrvStart in 2000. SrvStart allows you to run an ordinary Windows executable as a Windows service. - GitHub - rozanski/srvstart: This repository ...

Instructions: [https://www.howtogeek.com/50786/using-srvstart-to-run-any-application-as-a-windows-service/](https://www.howtogeek.com/50786/using-srvstart-to-run-any-application-as-a-windows-service/)

- Write configuration file: _E:\juliasrv.ini_

```julia
[julia_srv]
startup=C:\Julia\1.4.0_x64\bin\julia.exe C:\test_julia_server\http_server.jl
shutdown_method=winmessage

```

- Command prompt: create server (with no slashes in config file):

`sc create julia_srv Displayname= "julia_srv" binpath= "srvstart.exe julia_srv -c E:juliasrv.ini" start= auto`  
`sc start julia_srv`

```julia
SERVICE_NAME: julia_srv
        TYPE : 10 WIN32_OWN_PROCESS
        STATE : 2 START_PENDING
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE : 0 (0x0)
        SERVICE_EXIT_CODE : 0 (0x0)
        CHECKPOINT : 0x1
        WAIT_HINT : 0x1
        PID : 4732
        FLAGS :

```

- Trying to start it from Services window - it just closes immediately

![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/63e7683fc73c1310cccb6693924e649c1a95321f.png)

- Delete server:  
`sc delete julia_srv`

- Check log file - empty

* * *

Any ideas?

Should I write custom service app and embedd Julia call into it?

---

<div class="post-metadata">

### Author: ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)
#### Post date: [April 9, 2020, 12:51pm UTC](https://discourse.julialang.org/t/run-julia-server-as-windows-service-to-work-after-user-log-out/37261/2 "2020-04-09T12:51:09Z")

</div>

Finally, get it to work by setting admin user and password under Lon On property of `julia_server_nssm` in Services window.

---

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [February 28, 2022, 9:54pm UTC](https://discourse.julialang.org/t/run-julia-server-as-windows-service-to-work-after-user-log-out/37261/3 "2022-02-28T21:54:16Z")

</div>

> [@sairus7](#):
>
> sc start julia\_srv

Thanks for your contribution here. It helped me a lot.

I’d like to comment on your soulution and to add another possibility that worked for me.  
I think the reason that supplying your credentials to start the service assures that julia finds all required resources - in your case the packages.

A cleaner way, which I manged to get working today, is probably that you save your julia files in a directory that is available to the service (which I think you did) and to call

```julia
using Pkg
Pkg.instantiate()

```

at the beginning of the script. I used NSSM to get it running without supplying my credentials.

EDIT: I found the solution by setting the log files for out and error in the nssm dialog box. The error log showed the information about missing packages.
