1

I've made a simple web server with Ktor using build system Gradle. My build.gradle file applies the application plugin:

application {
    mainClass.set("io.ktor.server.netty.EngineMain")
}

So I can use ./gradlew run to run the web server

This works, but I would like to deploy my web server as a service on my VPS, because I would like the web app to restart automatically when Ubuntu reboots.

I don't know so much about Linux internals and Googling around I'm quite confused by how I should confront this.

I gathered that I should maybe define a init.d task, but how? Can I just write a bash script which runs ~/MyApp/gradlew run? Where do I put it?

init.d is the old, deprecated system for starting daemons; is has been supplanted by upstart. Upstart has the advantage of being far easier to configure and allows proper sequencing of task initialization.

I gathered that I should use upstart, but how?

Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd.

I gathered that I should use systemd, but how?

1
  • 1
    You would need to create a new SystemD service to execute your webserver. We would need to know the full path to gradlew and such in order ot make the service work. (I'm on my phone or I'd write an example) – Thomas Ward Feb 6 at 13:21
1

You can do it by using SystemD. First of all, change your working directory to /etc/systemd/system. That's where all the SystemD services are. Make sure your executable is accessible by SystemD. Next, create a .service file in the current directory (with root priveleges). Maybe something like webserver.service. Then, edit the file (with root priveleges), and write something like this:

[Unit]
Description=Service Description
Type=simple
ExecStart=/path/to/gradlew /path/to/executable

[Install]
WantedBy=multi-user.target

Make sure to change the description to something you can recognize, and set the /path/to/executable to your executable. Next, run systemctl enable yourservicename (with root priveleges). Reboot to check if it works. I hope this helps, let me know if it does!

5
  • 1
    You need the full path to gradlew if they're executing it of a working directory. – Thomas Ward Feb 6 at 14:12
  • Thank you! That looks surprisingly straightforward. – Maarten Feb 6 at 16:25
  • "Make sure your executable is accessible by SystemD" How do I verify this? Should the files in my project be flagged readable by a certain user? – Maarten Feb 6 at 16:27
  • 1
    Maybe that wasnt the best way to put it. I meant don't use characters like "~" for your home folder, use the full path. – me is me Feb 6 at 18:35
  • 1
    And I'm glad to have been able to help! – me is me Feb 6 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.