1

I'm new in Dart lang, also new in API services on linux.
My question is, how to I keep the Dart service active in linux? And how can I do it to recycle if I have a problem with the service?

I need to run in crontab?

1 Answers1

6

You can create a systemd service for you Aqueduct and enable it to run automatically when you server are started. There are a lot of options for systemd service but I have tried to make an example for you with you requirements:

[Unit]
Description=Dart Web Server
Wants=network-online.target
After=network-online.target

[Service]
Restart=always
ExecStart=/opt/dart-sdk/bin/dart bin/main.dart
WorkingDirectory=/tmp/web/my_project
User=webserver_user

[Install]
WantedBy=multi-user.target

Save this as /etc/systemd/system/name_of_your_service.service

Run hereafter the following commands:

  • systemctl daemon-reload
    • This will ensure the latest changes to you available services are loaded into systemd.
  • systemctl start name_of_your_service.service
    • This will start you service. You can stop it with "stop" and restart it with "restart".
  • systemctl enable name_of_your_service.service
    • This will enable the service so it will start after boot. You can also "disable" it.

Another good command is status command where you can see some information about your service (e.g. is it running?) and some of the latest log events (from stdout):

systemctl status name_of_your_service.service

Let me go through the settings I have specified:

  • "Wants"/"After" ensures that the service are first started after a network connection has been established (mostly relevant for when the service should start under the boot sequence).
  • "Restart" specifies what should happen if the dart process are stopped without using "systemctl stop". With "always" the service are restarted no matter how the program was terminated.
  • "ExecStart" the program which we want to keep running.
  • "User" is the user your want the service to run as.
  • The "WantedBy" part are relevant for the "systemctl enable" part and specifies when the service should be started. Use multi-user.target here unless you have some specific requirements.

Again, there are lot of options for systemd services and you should also check out journalctl if you want to see stdout log output for you service.

julemand101
  • 28,470
  • 5
  • 52
  • 48