Jenkins : Installing Jenkins as a Unix daemon

Linux startup script

daemonize

daemonize is a very simple program that allows you to launch Jenkins (or any arbitrary program) as a daemon. For people without much Unix experience, this is recommended.

daemontools

daemontools is another possibility. This is more complicated to set up and it doesn't really follow the normal Unix convention, but it offers daemon control (like start/stop), log capturing, as well as automatic restart when the service dies.

Shell script

        # Daemonize
        echo -n "Starting $SERVICE: "
        touch /var/run/jenkins.pid
        chown jenkins:jenkins /var/run/jenkins.pid
        su -s /bin/sh jenkins -c "
                cd /
                JENKINS_HOME=/var/lib/jenkins exec setsid /usr/bin/java   \
                        -jar /usr/share/java/jenkins/jenkins.war          \
                        $JENKINS_OPTS                                    \
                </dev/null >>/var/log/jenkins/console_log 2>&1 &
                echo \$! >/var/run/jenkins.pid
                disown \$!
                "

Adjust the above for your need – it is the part of my init scripts.

Java Service Wrapper

The The Java Service wrapper is a wrapper that works both on Unix and Windows. It requires some set up but when it is setup it can be used as any other unix service (like start/stop).

This example will install a Jenkins instance in /home/jenkins on a debian machine. The Jenkins configuration is stored in /home/jenkins/data. Jenkins will be running on port 8070 and as the jenkins user.

  1. Select and download a JSW package from the JSW wrapper download page.
  2. Unpack the package into the /home/jenkins path.
  3. Create /home/jenkins/tmp as the temporary directory for JSW.
  4. Download the jenkins.war file into /home/jenkins/lib.
    • If you have plugins that you would like to use, download them into /home/jenkins/data/plugins.
  5. Copy the /home/jenkins/src/conf/wrapper.conf.in to /home/jenkins/conf/wrapper.conf and change the below configuration properties in the file.

    wrapper.java.classpath.1=../lib/jenkins.war
    wrapper.java.classpath.2=../lib/wrapper.jar
    
    wrapper.java.additional.1=-DJENKINS_HOME=../data
    wrapper.java.additional.2=-Djava.io.tmpdir=../tmp
    wrapper.java.additional.3=-server
    
    # Set directly the time zone of the JVM, by setting this we do not rely on the time zone of the server.
    # Uncomment the following line if needed.
    # wrapper.java.additional.4=-Duser.timezone=America/Los_Angeles
    
    wrapper.app.parameter.1=Main
    wrapper.app.parameter.2=--httpPort=8070
    
    wrapper.ping.timeout=300
    wrapper.jvm_exit.timeout=60
    
    wrapper.console.title=Jenkins Continuous build server
    
  6. Verify the wrapper configuration by starting Jenkins through /home/jenkins/bin/testwrapper console. Jenkins should be accessible through http://localhost:8070, if not check the console output for errors.
  7. Copy the /home/jenkins/src/bin/sh.script.in to /etc/init.d/jenkins and change the below configuration properties in the file.

    APP_NAME="Jenkins"
    APP_LONG_NAME="Jenkins Continuous build server"
    
    WRAPPER_CMD="/home/jenkins/bin/wrapper"
    WRAPPER_CONF="/home/jenkins/conf/wrapper.conf"
    
    PIDDIR="/home/jenkins/bin"
    
    RUN_AS_USER=jenkins
    
  8. Verify installation by starting the daemon through /etc/init.d/jenkins start. Make sure that /etc/init.d/jenkins has the correct permissions; normally 755 is ok for this.

    Please feel free to add your suggestion here

Linux service - systemd

This is another way to start Jenkins as Linux service, also on system startup.

Fedora

I tested it on Fedora distribution, but it should work with other too. It needs some minimal experience with Linux superuser (sudo) and preferably basic Vi commands.

  1. Download jenkins.war file to any convenient destination, eg. /home/jenkins_user
  2. Create service file /etc/systemd/system/jenkins.service and paste following there:

    [Unit]
    Description=Jenkins Daemon
    
    [Service]
    ExecStart=/usr/bin/java -jar /home/jenkins_user/jenkins.war
    User=jenkins_user
    
    [Install]
    WantedBy=multi-user.target
    
  3. Reload service manager: systemctl daemon-reload
  4. That's it! Now manage the service:

    systemctl start jenkins.service      # starts the service manually
    systemctl stop jenkins.service
    systemctl restart jenkins.service
    systemctl enable jenkins.service     # enable the service to start with system startup;
                                         # please note that it does not start the service instantly
    systemctl disable jenkins.service    # disable automatic start
    
  5. If anything goes wrong, use journalctl command (and scroll to bottom) to see any error messages.
  6. You may want to redirect or suppress Jenkins output, otherwise you can kill Journal log. Change jenkins.helper to eg. java -jar /home/jenkins_user/jenkins.war > /dev/null &

openSUSE

This variation of the Fedora steps worked for me on openSUSE 15.0. It requires some minimal experience with Linux superuser (sudo) and basic text editor commands.

  1. Create service file /usr/lib/systemd/system/jenkins.service and paste the following there:

    [Unit]
    Description=Jenkins Daemon
    
    [Service]
    ExecStart=/usr/bin/java -jar /usr/lib/jenkins/jenkins.war
    User=jenkins
    
    [Install]
    WantedBy=multi-user.target
  2. Make a symbolic link from /etc/systemd/system/jenkins.service to /usr/lib/systemd/system/jenkins.service
    ln -s /usr/lib/systemd/system/jenkins.service /etc/systemd/system/jenkins.service
  3. Reload service manager

    systemctl daemon-reload
  4. That's it! Now manage the service:

    systemctl start jenkins.service      # starts the service manually
    systemctl stop jenkins.service
    systemctl restart jenkins.service
    systemctl enable jenkins.service     # enable the service to start with system startup;
                                         # please note that it does not start the service instantly
    systemctl disable jenkins.service    # disable automatic start
    
  5. If anything goes wrong, use journalctl command (and scroll to bottom) to see any error messages.
  6. You may want to redirect or suppress Jenkins output, otherwise you can kill Journal log. Change jenkins.helper to eg.

    java -jar /usr/lib/jenkins/jenkins.war > /dev/null &

Further Configuration

For further systemd configuration like auto restart, please refer to http://www.freedesktop.org/software/systemd/man/systemd.service.html

Examples

Ubuntu Linux

Init Script for Jenkins Continuous Integration Engine on Ubuntu Linux

SuSE Linux

Init Script for Jenkins/Hudson Continuous Integration Engine on SuSE Linux