Recently I’ve had to move an old Play web application that was previously running on Windows as a service under YAJSW to Ubuntu in AWS. It’s been a long time since I’ve done much in linux and in that time, Ubuntu has apparently moved from init.d to services. After reading a bit about services I created the following /etc/systemd/system/play.conf:
[Unit] Description=Your service name After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre Environment='_JAVA_OPTIONS=-XX:PermSize=512m -XX:MaxPermSize=1024m -Dprecompiled=true' ExecStart=/opt/play/play start /yourproject --%yourprofile ExecStop=/opt/play/play stop /yourproject --%yourprofile RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
This worked first time!
Starting at the top, following the description we specify the service to run after network configuration has taken place. The service itself is “forking” so systemd considers the service started once the process forks and the parent has exited. My Ubuntu AMI has multiple Java versions so we specify that we intend to run Play under 7. Because of the the different ways of running play applications e.g. launching through Python or via Java directly or as a wrapped service, it’s sometimes confusing as to how to pass the necessary properties into it. While I’ve left the profile e.g. prod, stage etc. to the start and stop execs, I’ve added everything else that I require to “_JAVA_OPTIONS” including settings for perm gen memory and specified that I require Play to use the pre-compiled templates. Next are properties related to restarting the service. Finally we have the install section where basically specify that the service should be included at about run level 3.
This is the basic setup but for security don’t forget to run the service as a specific user using User=, Group= and appropriate UMask.