Skip to content

Instantly share code, notes, and snippets.

@BenWhitehead
Created March 7, 2014 23:09
Show Gist options
  • Save BenWhitehead/9422087 to your computer and use it in GitHub Desktop.
Save BenWhitehead/9422087 to your computer and use it in GitHub Desktop.
systemd service files for running TeamCity (create in /usr/lib/systemd/system)
[Unit]
Description=TeamCity Build Agent
After=network.target
[Service]
Type=forking
PIDFile=$AGENT_HOME/logs/buildAgent.pid
ExecStart=/usr/bin/sudo -u teamcity $AGENT_HOME/bin/agent.sh start
ExecStop=/usr/bin/sudo -u teamcity $AGENT_HOME/bin/agent.sh stop
[Install]
WantedBy=multi-user.target
[Unit]
Description=TeamCity Server
After=network.target
[Service]
Type=forking
PIDFile=$TEAMCITY_HOME/logs/teamcity-server.pid
; Make sure the CATALINA_PID env variable is setup in $TEAMCITY_HOME/bin/catalina.sh
ExecStart=/usr/bin/sudo -u teamcity $TEAMCITY_HOME/bin/teamcity-server.sh start
ExecStop=/usr/bin/sudo -u teamcity $TEAMCITY_HOME/bin/teamcity-server.sh stop
[Install]
WantedBy=multi-user.target
@cm-rudolph
Copy link

Instead of using sudo, you should define a separate line in the service section containing User=teamcity.

@jordigg
Copy link

jordigg commented Feb 3, 2016

Do you guys have the same problem?
http://serverfault.com/questions/660063/teamcity-build-agent-gets-killed-by-systemd-when-upgrading

TeamCity Agent service is killed once the updated is performed. Do you know any way to overcome this issue?

@Sauraus
Copy link

Sauraus commented Mar 1, 2017

The kill is part of the auto-update process with a proper systemctl config file it will restart just fine, yhis is the systemctl control file generated from our TeamCity Chef cookbook:

[Unit]
Description=TeamCity Agent
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/opt/teamcity-agent/bin/agent.sh start
Type=forking
RemainAfterExit=yes
User=teamcity
Group=users
SyslogIdentifier=teamcity_agent
PrivateTmp=yes
PIDFile=/opt/teamcity-agent/logs/buildAgent.pid
ExecStop=/opt/teamcity-agent/bin/agent.sh stop
RestartSec=5
Restart=on-failure

@alst74
Copy link

alst74 commented Sep 11, 2017

How do you normally install these unitfiles? Copy them to /etc/system/systemd? When I did the services was "masked". Basically I was trying this way:

cp teamcity*.service /etc/systemd/system/
chmod 664 /etc/systemd/system/teamcity*.service
systemctl enable teamcity-server
systemctl enable teamcity-agent

[root@master ~]# systemctl list-unit-files|grep -i teamcity
teamcity-agent.service                        masked
teamcity-server.service                       masked

@oschlueter
Copy link

@alst74:

$ cp teamcity-agent.service /lib/systemd/system
$ systemctl enable teamcity-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/teamcity-agent.service to /usr/lib/systemd/system/teamcity-agent.service.
$ systemctl list-unit-files|grep -i teamcity
teamcity-agent.service                        enabled 

@rpetti-ot
Copy link

rpetti-ot commented Aug 29, 2022

I've tried multiple variations on the systemd unit file, but it still doesn't seem to want to update. Using @Sauraus's it just ends up in a restart loop, never actually finishing the upgrade. We need to always log in and manually start the service at least once with the agent.sh script in order for the upgrade to complete.

Does anyone have a functional unit file for this?

Edit:
The following seems to work for me:

[Unit]
Description=TeamCity Build Agent
After=network.target

[Service]
User=archiv
Type=forking
RemainAfterExit=yes
SuccessExitStatus=0 143
Restart=on-failure
PIDFile=/opt/BuildAgent_TC/buildAgent/logs/buildAgent.pid
ExecStart=/opt/BuildAgent_TC/buildAgent/bin/agent.sh start
ExecStop=/opt/BuildAgent_TC/buildAgent/bin/agent.sh stop

[Install]
WantedBy=multi-user.target

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