Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derjohn/a535640f98074911ac5840cac79bc1cf to your computer and use it in GitHub Desktop.
Save derjohn/a535640f98074911ac5840cac79bc1cf to your computer and use it in GitHub Desktop.

Howto manage a tomcat8/9 instance as a user (non-root)

Install tomcat8/9

Figure out yourself, probably use your distros package manager Create a user e.g. tomcat9user

Create an Instance

sudo -iu tomcat9user
tomcat9-instance-create -p 8081 -c 8006 ~/tomcat9specialservice-instance

Prepare Unit service file in the user's homedir

sudo loginctl enable-linger tomcat9user # root must enable the linger service for the user, otherwise autostart won't work
sudo -iu tomcat9user 
mkdir -p $HOME/.config/systemd/user
cat <<EOF > $HOME/.config/systemd/user/tomcat-user-instance@.service
[Unit]
Description=Tomcat User Instance - instance %i
After=syslog.target network.target

[Service]
Type=forking

ExecStart=${HOME}/%i/bin/startup.sh
ExecStop=${HOME}/%i/shutdown.sh

RestartSec=60
Restart=always
EOF

Start the Service and make it reboot savvy

sudo -iu tomcat9user 
XDG_RUNTIME_DIR=/run/user/$(id -u) systemctl --user daemon-reload
XDG_RUNTIME_DIR=/run/user/$(id -u) systemctl --user enable tomcat-user-instance@tomcat9specialservice --now
XDG_RUNTIME_DIR=/run/user/$(id -u) systemctl --user status tomcat-user-instance@tomcat9specialservice

Multi-Instance

The user may create multiple instances on different ports (higher than port 1024) with tomcat9-instance-create. The unit file is only needed once per user, with ... systemctl --user enable tomcat-user-instance@someOtherService --now multiple tomcat services can be managed with the same unit file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment