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

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
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
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
    @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:

Batch: enter
Batch: dir changed
Batch: exit

Looks like it cannot even execute Julia…


2. Using NSSM

  • 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

#2 Call batch file C:\test_julia_server\run_server.bat

  • 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:

  • Failed, so stop and delete the service
    nssm stop julia_server_nssm
    nssm remove julia_server_nssm

  • Check log file:

#1:

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:

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

  • 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

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

  • Write configuration file: E:\juliasrv.ini
[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

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

  • Delete server:
    sc delete julia_srv

  • Check log file - empty


Any ideas?

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

1 Like

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

1 Like

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

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.

1 Like