Tag Archives: Play Framework

My Play Framework Systemd script

Ubuntu deprecated Upstart so I had to turn to Systemd for my app controls in Ubuntu 18.04. In this script I set 2 environment variables (HOME and LANG), change directory to the app directory and starts the Play Framework application

01# Myapp systemd script
02#
03# Location:/lib/systemd/system/myapp.service
04#
05# Useful commands:
06#
07# Start Myapp:      systemctl start myapp.service
08# Stop Myapp:       systemctl stop myapp.service
09# Restart Myapp:    systemctl restart myapp.service
10# Show status:      systemctl status myapp.service
11# Enable start on boot: systemctl enable myapp.service
12# Disable start on boot:systemctl disable myapp.service
13#
14# List all services running: systemctl
15# Check config: systemd-analyze verify myapp.service
16#
17####################################################################################
18 
19[Unit]
20Description=Job that runs my app daemon
21 
22[Service]
23Type=forking
24Environment=HOME=/opt/myapp/app
25Environment=LANG=en_US.UTF-8
26ExecStartPre=/bin/bash -c 'cd /opt/myapp/app'
27ExecStart=/bin/bash -c 'bin/myapp -J-Xms256M -J-Xmx768m -J-server -Dhttp.port=80 -Dconfig.file=conf/application.conf -Dlogger.file=conf/application-logger.xml'
28 
29[Install]
30WantedBy=multi-user.target

The arguments for the Play service are what I normally use for AWS. You might need other settings

Tested on Ubuntu 18.04 and Play Framework 2.3

My Play Framework logrotate configuration

Here is another script I need to keep handy for the future. My logrotate script for Play Framework applications. The important thing here is the parameter “copytruncate”. I tried many times without it and the result was always the same: after the logrotate was done no more logging was made by the application. “copytruncate” solved this

1/opt/myapp/logs/*.log {
2  missingok
3  notifempty
4  size 50M
5  copytruncate
6  create 0644 root root
7  rotate 9999
8}

How to run logrotate manually:

1logrotate --force /etc/logrotate.d/myapp

Tested on Ubuntu 14.04 and Play Framework 2.3

My Play Framework Upstart script

Recently I had to create a upstart script for a Play application and this was the result of that. I put it here so I don’t have to start from scratch next time 🙂

########################################################################################################
#
#                                       Upstart Script
#
# Upstart script for a play application. Put this into a file like /etc/init/myapp.conf
#
# This could be the foundation for pushing play apps to the server using something like git-deploy
# By calling service play stop in the restart command and play-start in the restart command.
#
# Usage:
#   sudo start myapp
#   sudo stop myapp
#   sudo restart myapp
#   sudo status myapp
#
#########################################################################################################
 
description "Upstart script for MyApp"
author "Niklas Ottosson"
version "1.0"
 
# Set environment variables
env HOME=/my/app/dir/
env LANG=en_US.UTF-8
 
# Start and stop runlevels
start on runlevel [2345]
stop on runlevel [06]
 
# Respawn parameters with limit: dies 3 times within 60 seconds
respawn
respawn limit 3 60
 
# Change directory to current version of Tankmin
chdir /my/app/dir/
 
# Delete any stray PIDs
pre-start script
  rm -f ${HOME}/RUNNING_PID
end script
 
# Upstart logging (/var/log/upstart/myapp.log)
console none
 
# TEST ENVIRONMENT (arguments here are what I'm using for this particular app - you should use what works best for your app)
exec bin/myapp -J-Xms256M -J-Xmx768m -J-server -Dhttp.port=80 -Dconfig.file=conf/application.conf -Dlogger.file=conf/application-logger_PROD.xml

Tested in a production environment on Ubuntu 14.04 and Play Framework 2.3