1

I am executing below jenkins.war with below command

  jenkins -jar jenkins.war

But I want to specify to use below path while executing the war

 `/data/jenkins`

and it should run as jenkins user . Right now it is getting executed as root user and under /root directory

How can I achieve that ?

  • Did you create the user jenkins? – Panki Mar 19 at 9:45
  • Also, did you create /data dir? – user34720 Mar 19 at 12:11
0

I used environmental variables as below in script

JENKINS_HOME="/data/jenkins" JENKINS_WAR="/data/jenkins/jenkins.war"

and passed them to Java with -D option

java -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR

# ps -ef | grep java
root         5     1  0 Mar19 ?        00:05:10 /apps/java/jdk1.8.0_121/bin/java  -DJENKINS_HOME=/data/jenkins -jar /data/jenkins/jenkins.war
0

The Jenkins wiki discusses setting it up as a Unix daemon: Installing Jenkins as a Unix daemon. You have an RHEL tag in the question, and since RHEL 7 makes use of systemd, you could set up Jenkins to run as a systemd service. The steps to do this are shown below.

  1. First, you'll need to download the Jenkins WAR file and place it somewhere. I chose the location /opt/jenkins/jenkins.war.

  2. Next, you'll need to create/prepare the data directory you want to use, which is /data/jenkins.

  3. You can now create a system user account with the name jenkins:

    useradd -r jenkins
    
  4. Next, change the ownership of the Jenkins WAR file and data directory to this new user:

    chown -R jenkins:jenkins /opt/jenkins/
    chown -R jenkins:jenkins /data/jenkins/
    
  5. Next, define the systemd service by creating a new unit file:

    vi /etc/systemd/system/jenkins.service
    
    [Unit]
    Description=Jenkins Daemon
    
    [Service]
    ExecStart=/bin/java -jar /opt/jenkins/jenkins.war
    User=jenkins
    Environment=JENKINS_HOME=/data/jenkins
    
    [Install]
    WantedBy=multi-user.target
    
  6. Make systemd aware of the new unit by reloading it:

    systemctl daemon-reload
    
  7. Finally, start Jenkins:

    systemctl start jenkins
    

You should now be able to access Jenkins on port 8080. If firewalld is active, you'll need to allow the port by running firewall-cmd --add-port=8080/tcp on the system.

The Jenkins logs can now be seen with journalctl _SYSTEMD_UNIT=jenkins.service. Running ps -ef | grep jenkins will show that it's running as the jenkins user:

jenkins   1749     1  7 11:04 ?        00:00:35 /bin/java -jar /opt/jenkins/jenkins.war

As an added bonus, run systemctl enable jenkins if you want the Jenkins service to be automatically started on system boot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.