Bird
Raised Fist0
Jenkinsdevops~10 mins

Agent availability and offline handling in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Agent availability and offline handling
Agent starts
↓
Agent connects to Jenkins Master
↓
Agent status: Online
↓
Agent executes jobs
↓
Agent disconnects or fails
↓
Agent status: Offline
↓
Jenkins Master detects offline
↓
Handle offline: retry, notify, or mark unavailable
↓
Agent reconnects or manual intervention
This flow shows how a Jenkins agent connects, runs jobs, goes offline, and how Jenkins handles the offline state.
Execution Sample
Jenkins
agent.connect()
if agent.is_online():
    agent.run_job(job)
else:
    jenkins.notify_admin(agent)
    agent.retry_connection()
This code tries to connect an agent, runs a job if online, otherwise notifies admin and retries connection.
Process Table
StepActionAgent StatusResultNotes
1Agent starts and attempts connectionConnectingConnection attempt sentAgent tries to connect to Jenkins master
2Agent connection successfulOnlineAgent ready for jobsAgent status changes to online
3Agent receives jobOnlineJob startedAgent begins executing job
4Agent job runningOnlineJob in progressAgent is busy running job
5Agent loses connectionOfflineConnection lost detectedNetwork or agent failure occurs
6Jenkins detects offline agentOfflineAdmin notifiedJenkins marks agent offline and sends alert
7Jenkins retries agent connectionRetryingRetry attempt sentJenkins tries to reconnect agent
8Agent reconnects successfullyOnlineAgent ready againAgent back online and ready for jobs
9Agent remains offline after retriesOfflineMarked unavailableAgent marked as unavailable for scheduling jobs
💡 Agent either reconnects successfully or is marked unavailable after retries
Status Tracker
VariableStartAfter Step 2After Step 5After Step 8Final
agent.statusDisconnectedOnlineOfflineOnlineOnline or Offline
jenkins.admin_notifiedFalseFalseTrueTrueTrue or False
agent.retry_attempts0012Max retries or 0
Key Moments - 3 Insights
Why does the agent status change to offline at step 5?
Because the agent lost connection to the Jenkins master, Jenkins detects this and updates the status to offline as shown in execution_table row 5.
What happens if the agent does not reconnect after retries?
Jenkins marks the agent as unavailable for scheduling jobs, as shown in execution_table row 9, preventing jobs from being assigned to a non-responsive agent.
How does Jenkins notify about an offline agent?
At step 6, Jenkins sends a notification to the admin to alert them about the offline agent, as indicated in the 'Result' and 'Notes' columns of execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the agent status at step 3?
AOnline
BOffline
CRetrying
DDisconnected
💡 Hint
Check the 'Agent Status' column in execution_table row 3.
At which step does Jenkins notify the admin about the offline agent?
AStep 4
BStep 7
CStep 6
DStep 9
💡 Hint
Look at the 'Result' and 'Notes' columns in execution_table row 6.
If the agent never reconnects, what is the final agent status?
ARetrying
BOffline
COnline
DDisconnected
💡 Hint
Refer to the 'Final' column in variable_tracker for agent.status and execution_table row 9.
Concept Snapshot
Jenkins agents connect to the master to run jobs.
Agent status can be Online or Offline.
If offline, Jenkins detects and notifies admin.
Jenkins retries connection automatically.
If retries fail, agent is marked unavailable.
This ensures jobs run only on available agents.
Full Transcript
In Jenkins, agents connect to the master to execute jobs. When an agent starts, it attempts to connect. If successful, its status is Online and it can run jobs. If the agent loses connection, Jenkins detects this and marks the agent Offline. Jenkins then notifies the administrator and tries to reconnect the agent automatically. If the agent reconnects, it becomes Online again and resumes job execution. If it fails to reconnect after retries, Jenkins marks the agent as unavailable to prevent job assignment. This process ensures Jenkins only schedules jobs on agents that are available and responsive.

Practice

(1/5)
1. What does it mean when a Jenkins agent is marked as offline?
easy
A. The agent is connected and ready to accept jobs.
B. The agent is not available to run any jobs.
C. The agent is running a job currently.
D. The agent is permanently deleted from Jenkins.

Solution

  1. Step 1: Understand agent status meaning

    In Jenkins, an agent marked offline means it is not available to run jobs.
  2. Step 2: Differentiate from other statuses

    Online agents are connected and ready; offline means unavailable temporarily or for maintenance.
  3. Final Answer:

    The agent is not available to run any jobs. -> Option B
  4. Quick Check:

    Offline agent = unavailable [OK]
Hint: Offline means agent can't run jobs now [OK]
Common Mistakes:
  • Confusing offline with running a job
  • Thinking offline means deleted
  • Assuming offline means ready
2. Which of the following is the correct way to mark a Jenkins agent as offline using the Jenkins CLI?
easy
A. jenkins-cli offline-agent --name agent1
B. java -jar jenkins-cli.jar offline-agent agent1
C. java -jar jenkins-cli.jar offline-agent --agent agent1
D. java -jar jenkins-cli.jar disconnect-node agent1

Solution

  1. Step 1: Recall Jenkins CLI command for offline

    The correct command to mark an agent offline is disconnect-node with the agent name as argument.
  2. Step 2: Check syntax correctness

    Options A, B, and C use incorrect commands or flags; only java -jar jenkins-cli.jar disconnect-node agent1 uses the correct command and syntax.
  3. Final Answer:

    java -jar jenkins-cli.jar disconnect-node agent1 -> Option D
  4. Quick Check:

    disconnect-node command marks offline [OK]
Hint: Use 'disconnect-node' command with agent name [OK]
Common Mistakes:
  • Using wrong command names like offline-agent
  • Adding incorrect flags
  • Confusing CLI syntax
3. Given this Jenkins pipeline snippet:
node('agent1') {
  echo "Agent status: ${env.NODE_NAME}"
}

If agent1 is offline, what will happen when this pipeline runs?
medium
A. The pipeline will fail immediately with an error.
B. The pipeline will run on the master node instead.
C. The pipeline will wait until agent1 comes online.
D. The pipeline will skip the node block and continue.

Solution

  1. Step 1: Understand node block behavior

    The node('agent1') block waits for the specified agent to be available before running.
  2. Step 2: Check what happens if agent is offline

    If agent1 is offline, Jenkins will wait until it comes online or timeout occurs.
  3. Final Answer:

    The pipeline will wait until agent1 comes online. -> Option C
  4. Quick Check:

    node waits for agent online [OK]
Hint: node waits for specified agent to be online [OK]
Common Mistakes:
  • Assuming pipeline fails immediately
  • Thinking it runs on master automatically
  • Believing it skips the node block
4. You have a Jenkins agent that should be offline for maintenance, but jobs are still assigned to it. What is the most likely cause?
medium
A. The agent was not properly marked offline in Jenkins configuration.
B. The agent is offline but Jenkins is ignoring the status due to a plugin bug.
C. The agent was marked offline but the 'Accept tasks' option is still enabled.
D. The jobs are configured to run only on the master node.

Solution

  1. Step 1: Check agent offline marking

    If jobs are still assigned, likely the agent was not properly marked offline in Jenkins UI or configuration.
  2. Step 2: Eliminate other causes

    The agent was marked offline but the 'Accept tasks' option is still enabled. is incorrect because offline agents do not accept tasks; The agent is offline but Jenkins is ignoring the status due to a plugin bug. is rare; The jobs are configured to run only on the master node. is unrelated.
  3. Final Answer:

    The agent was not properly marked offline in Jenkins configuration. -> Option A
  4. Quick Check:

    Proper offline marking stops job assignment [OK]
Hint: Ensure agent is correctly marked offline in Jenkins UI [OK]
Common Mistakes:
  • Assuming offline agent still accepts tasks
  • Blaming plugins without checking config
  • Confusing job node assignment
5. You want to create a Jenkins pipeline that automatically skips running on an agent if it is offline and instead runs on the master node. Which scripted pipeline snippet achieves this?
hard
A. if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) { node('agent1') { echo 'Running on agent1' } } else { node('master') { echo 'Running on master' } }
B. node('agent1') { if (agent1.isOnline()) { echo 'Running on agent1' } else { node('master') { echo 'Running on master' } } }
C. try { node('agent1') { echo 'Running on agent1' } } catch (Exception e) { node('master') { echo 'Running on master' } }
D. node('agent1') { echo 'Running on agent1' } node('master') { echo 'Running on master' }

Solution

  1. Step 1: Check agent online status in pipeline

    if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) { node('agent1') { echo 'Running on agent1' } } else { node('master') { echo 'Running on master' } } correctly checks if 'agent1' is online using Jenkins API before choosing node.
  2. Step 2: Verify fallback to master node

    If agent1 is offline, if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) { node('agent1') { echo 'Running on agent1' } } else { node('master') { echo 'Running on master' } } runs the job on 'master' node, matching the requirement.
  3. Step 3: Analyze other options

    node('agent1') { if (agent1.isOnline()) { echo 'Running on agent1' } else { node('master') { echo 'Running on master' } } } uses invalid syntax; try { node('agent1') { echo 'Running on agent1' } } catch (Exception e) { node('master') { echo 'Running on master' } } relies on exception which is not best practice; node('agent1') { echo 'Running on agent1' } node('master') { echo 'Running on master' } runs both nodes unconditionally.
  4. Final Answer:

    if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) { node('agent1') { echo 'Running on agent1' } } else { node('master') { echo 'Running on master' } } -> Option A
  5. Quick Check:

    Check agent online status before node selection [OK]
Hint: Check agent online status via Jenkins API before node [OK]
Common Mistakes:
  • Using invalid syntax to check agent status
  • Relying on exceptions to handle offline agents
  • Running both nodes regardless of status
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard