How to run a script automatically on system startup in centos

I've successfully installed TeamCity ib CentOS 6.2 Minimal. I'm stuck on trying to run a script automatically on system startup:

/opt/TeamCity/bin/teamcity-server.sh start

I've googled around and tried various methods all of which seems to fail. Most of the methods points to adding the startup script to one of the rc.local files:

/etc/rc.d/rc.local
/etc/rc.local

And the contents of rc.local contains the line:

/opt/TeamCity/bin/teamcity-server.sh start

None of which will start the teamcity server

There are no problems if i run the script manually.

Seems simple but how do i get a shell script to auto run?


If the teamcity start script is a LSB-compliant init script (that is, if it provides start, stop and other arguments), you can just copy the script to /etc/init.d folder and run (as root):

# chkconfig --add <script_name>
# chkconfig <script_name> on

A symbolic link for the file should be OK too. So try (as root):

# ln -s /opt/TeamCity/bin/teamcity-server.sh /etc/init.d/teamcity-server
# chkconfig --add teamcity-server
# chkconfig teamcity-server on

If it doesn't start on initialization, but starting manually without problems, you should check:

  1. permissions (maybe the program can not be run as root. Try to log on as root and run it), and;
  2. your PATH (maybe the script need something but it does not know where it is).

To print the content of your PATH just run:

echo $PATH

To check if the reason to the script not start is the PATH, try:

$ cd /
$ PATH= /opt/TeamCity/bin/teamcity-server.sh

This command will temporary clear your PATH and run the command. If the command need some specific info on the PATH it will fail (be careful to run exactly as stated above, THERE IS a space between PATH= and the remaining of the command).


in Fedora 17 command

# chkconfig --add teamcity-server

say: service teamcity-server does not support chkconfig you should add header like:

#! /bin/bash
# chkconfig: - 10 90

only after that run first command.


When the script is run manually, it works correctly. But, when run automatically it doesn't work.

I've found the culprit: TeamCity runs under the user teamcity. It must be present in the /etc/sudoers file and you should add, using visudo a line like this:

Defaults:teamcity !requiretty