New for Ubuntu 16.04 (Xenial Xerus) is systemd which replaces Upstart as the default init system.
systemd basics
The basic object that systemd manages is the unit which can be of different types but when it comes to running our Play app we will need to create a service.
The systemctl command is used to manage systemd services, similar to the old service command. E.g. to start Nginx you now type:
sudo systemctl start nginx.service
Custom unit files should go in the /etc/systemd/system directory and you should always run the following command after creating new unit files or modifying existing ones:
sudo systemctl daemon-reload
Run levels have been replaced with systemd units called targets. Target unit files end with the .target file extension and they are used to group other units.
A Play Framework unit file
Here is a basic Play Framework systemd unit file. It makes use of the EnvironmentFile directive which can be used to set environment variables. It is possible to set environment variables in the unit file directly but using a separate file takes some of the clutter out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ADDRESS=127.0.0.1 | |
PORT=9000 | |
APPLY_EVOLUTIONS=true | |
APPLICATION_SECRET=my_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=My play app | |
After=network.target | |
[Service] | |
EnvironmentFile=/path/to/app/conf/env | |
PIDFile=/path/to/app/RUNNING_PID | |
WorkingDirectory=/path/to/app | |
ExecStart=/path/to/app/bin/play -Dhttp.address=${ADDRESS} -Dhttp.port=${PORT} -Dplay.evolutions.db.default.autoApply=${APPLY_EVOLUTIONS} -J-server | |
Restart=on-failure | |
User=play_user | |
Group=play_user | |
# See http://serverfault.com/a/695863 | |
SuccessExitStatus=143 | |
[Install] | |
WantedBy=multi-user.target |
Deploying the unit file
- Create the env file in your app’s conf directory and unit file in /etc/systemd/system
- Set the file permissions
sudo chmod 664 /etc/systemd/system/play.service
- Reload systemd
sudo systemctl daemon-reload
You can now start and stop your play app using systemctl
sudo systemctl start play.service sudo systemctl stop play.service
Leave a Reply