How to Automatically Execute Shell Script on CentOS 6.2 Boot: TeamCity Server Startup Guide


6 views

When trying to run /opt/TeamCity/bin/teamcity-server.sh start automatically on CentOS 6.2 startup, many developers first attempt using rc.local. While this method works for simple scripts, it often fails for services like TeamCity because:

# Environment variables may not be available
# Script execution order isn't guaranteed
# Service dependencies might not be ready
# No proper service management (start/stop/status)

The correct approach is to create a proper init script in /etc/init.d/. Here's a complete example for TeamCity:

#!/bin/bash
# chkconfig: 2345 90 10
# description: TeamCity Build Server

TEAMCITY_USER=teamcity
TEAMCITY_HOME=/opt/TeamCity

case "$1" in
  start)
    echo "Starting TeamCity..."
    su - $TEAMCITY_USER -c "$TEAMCITY_HOME/bin/teamcity-server.sh start"
    ;;
  stop)
    echo "Stopping TeamCity..."
    su - $TEAMCITY_USER -c "$TEAMCITY_HOME/bin/teamcity-server.sh stop"
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  status)
    ps -ef | grep teamcity | grep -v grep
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0

1. Save the script as /etc/init.d/teamcity

sudo vi /etc/init.d/teamcity

2. Set proper permissions:

sudo chmod 755 /etc/init.d/teamcity

3. Configure the service to start on boot:

sudo chkconfig --add teamcity
sudo chkconfig teamcity on

If your TeamCity installation requires specific environment variables, create a /etc/sysconfig/teamcity file:

# TeamCity environment configuration
JAVA_HOME=/usr/java/default
TEAMCITY_DATA_PATH=/var/TeamCity
export JAVA_HOME TEAMCITY_DATA_PATH

Then modify your init script to source this file:

# Add this near the top of /etc/init.d/teamcity
[ -f /etc/sysconfig/teamcity ] && . /etc/sysconfig/teamcity

If the service still doesn't start, check these logs:

# System logs
tail -n 50 /var/log/messages

# TeamCity logs
tail -n 50 /opt/TeamCity/logs/teamcity-server.log

Common issues include:

  • File permissions (run as correct user)
  • Missing environment variables
  • Port conflicts (default port 8111)
  • Java not being found

While /etc/rc.local is often recommended for simple startup tasks in CentOS, it has several limitations that might explain why your TeamCity server isn't starting:

# Common issues with rc.local:
# 1. Environment variables not being set
# 2. Script executes before required services are available
# 3. No proper logging of startup failures
# 4. Runs as root by default, which might cause permission issues

For production systems, creating a proper init script is the recommended approach. Here's how to implement it for TeamCity:

#!/bin/bash
#
# chkconfig: 345 99 01
# description: TeamCity Build Server

case "$1" in
  start)
    echo "Starting TeamCity Server"
    su - teamcity -c "/opt/TeamCity/bin/teamcity-server.sh start"
    ;;
  stop)
    echo "Stopping TeamCity Server"
    su - teamcity -c "/opt/TeamCity/bin/teamcity-server.sh stop"
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

1. Save the script as /etc/init.d/teamcity

sudo vi /etc/init.d/teamcity
sudo chmod +x /etc/init.d/teamcity

2. Add the service to startup sequence:

sudo chkconfig --add teamcity
sudo chkconfig teamcity on

If the service still doesn't start, check these troubleshooting steps:

# View startup logs:
journalctl -xe

# Test the script manually:
sudo service teamcity start

# Check environment variables:
env | grep -i java
env | grep -i teamcity

For newer CentOS versions, systemd provides better control:

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

[Service]
Type=forking
User=teamcity
ExecStart=/opt/TeamCity/bin/teamcity-server.sh start
ExecStop=/opt/TeamCity/bin/teamcity-server.sh stop
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save as /etc/systemd/system/teamcity.service and enable with:

sudo systemctl daemon-reload
sudo systemctl enable teamcity
sudo systemctl start teamcity

After implementation, verify the service status:

sudo chkconfig --list teamcity  # CentOS 6
sudo systemctl status teamcity  # CentOS 7+