Installing Jenkins as a Unix daemon

Skip to end of metadata
Go to start of metadata

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. 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 helper" file anywhere, eg. /home/jenkins_user/jenkins.helper
  3. Paste following there:
     java -jar /home/jenkins_user/jenkins.war &
    
    This helper is needed only to start Java in background, as it does not want to work inside .service file.
  4. Create service file /lib/systemd/system/jenkins.service and paste following there:
    [Unit]
    Description=Jenkins Daemon
    After=syslog.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/sh /home/jenkins_user/jenkins.helper
    User=jenkins_user
    
    [Install]
    WantedBy=multi-user.target
    
  5. Reload service manager: systemctl daemon-reload
  6. 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
    
  7. If anything goes wrong, use journalctl command (and scroll to bottom) to see any error messages.
  8. 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 &

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

Labels

Edit
init init Delete
script script Delete
daemon daemon Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Add Comment