如何在CentOS系统中通过Jenkins.war文件启动Jenkins服务
Hey there! Since you’ve already got the Jenkins.war file and a working Java environment set up on your CentOS system, getting Jenkins up and running is straightforward. Let’s walk through the steps:
1. Verify Java is properly installed first
Before launching Jenkins, double-check that your Java version is compatible (Jenkins recommends Java 11 or 17 right now). Run this command in your terminal:
java -version
You should see output showing a supported Java version. If not, you’ll need to adjust your Java setup, but since you mentioned it’s already installed, this is just a quick sanity check.
2. Basic Jenkins startup
Navigate to the directory where your jenkins.war file is located (or use the full file path directly). Then run:
java -jar jenkins.war
- The first time you start Jenkins, it’ll generate an initial admin password. Keep an eye on the terminal output—you’ll see a line pointing to the password file (usually
/var/lib/jenkins/secrets/initialAdminPassword) or the password string itself. Save this, you’ll need it to set up Jenkins in your browser. - By default, Jenkins runs on port 8080. If that port’s already in use, you can specify a different one with:
java -jar jenkins.war --httpPort=8081 # Replace 8081 with your preferred port
3. Run Jenkins in the background (persistent setup)
If you close the terminal window, the basic startup command will stop Jenkins. For a more persistent setup, use one of these methods:
Option A: Use nohup for quick background running
This command will run Jenkins in the background and log output to a file:
nohup java -jar jenkins.war > jenkins.log 2>&1 &
nohupkeeps the process running even after you log out- Output is redirected to
jenkins.log(you can check this file for status or errors later) - To stop Jenkins, find its process ID with
ps aux | grep jenkinsthen kill it withkill <PID>
Option B: Set up a systemd service (recommended for production)
This makes Jenkins a system service that starts on boot and can be managed with standard system commands:
- Create a systemd service file:
vi /etc/systemd/system/jenkins.service - Paste the following content (replace
/path/to/your/jenkins.warwith the actual path to your file, and adjust the user if you want to run Jenkins as a non-root user for security):[Unit] Description=Jenkins Continuous Integration Server After=network.target [Service] User=root ExecStart=/usr/bin/java -jar /path/to/your/jenkins.war --httpPort=8080 Restart=always RestartSec=10 [Install] WantedBy=multi-user.target - Save and exit the editor. Then reload systemd to recognize the new service:
systemctl daemon-reload - Start Jenkins:
systemctl start jenkins - Set it to start automatically on system boot:
systemctl enable jenkins - Check the service status anytime with:
systemctl status jenkins
4. Verify Jenkins is running
Open a web browser and navigate to http://[your-centos-server-ip]:[port] (replace the placeholders with your server’s IP and the port Jenkins is using—default is 8080). You should see the Jenkins setup page, where you’ll need to enter that initial admin password we mentioned earlier.
内容的提问来源于stack exchange,提问作者Programming-Lover

