Answers: <p>Assuming Jenkins is installed via a package which installs a systemd unit file:</p>
  <pre><code>systemctl restart jenkins
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-08-13T10:33:24.543Z
Answers: That restarts Jenkins, but it isn't a safe restart. All currently running jobs are aborted in a non-graceful way.
Answers: Michael Firth
Answers: CC BY-SA 4.0
Answers: 2021-08-16T23:00:33.647Z
Answers: That's not the case for my instance of Jenkins (2.289.3).  If I restart Jenkins with systemd (i.e. the command in my answer), any jobs which were running before the restart are resumed after Jenkins comes back online.
Answers: jayhendren
Answers: CC BY-SA 4.0
Answers: 2021-08-16T23:02:36.447Z
Answers: And after the restart, in my build output, I see (for instance) `Resuming build at Mon Aug 16 16:58:58 MDT 2021 after Jenkins restart`.
Answers: jayhendren
Answers: CC BY-SA 4.0
Answers: 2021-09-27T10:15:44.903Z
Answers: I don't see how that can be true in the generic case - my jobs are running on "agent" systems via custom build scripts. Jenkins could in theory restart the jobs after an "unclean" restart via systemd, but I don't see how it could resume them. As some of my jobs take 4 hours to run, having it restart it when it is 90% finished wouldn't be great.
Answers: Michael Firth
Answers: CC BY-SA 4.0
Answers: 2021-09-27T15:50:49.223Z
Answers: Are you running Pipeline jobs, or Freestyle?  Pipeline jobs are robust against Jenkins master restarts, but I don't know about Freestyle jobs.
Answers: jayhendren
Answers: CC BY-SA 4.0
Answers: 2021-09-28T09:55:52.280Z
Answers: All my jobs are Freestyle ones.
Answers: Michael Firth
Answers: CC BY-SA 4.0
Answers: 2021-08-11T16:06:02.070Z
Answers: 2021-08-11T16:06:02.070Z
Answers: jayhendren
Body: <p>There is an existing question here on how to safely restart Jenkins:</p>
  <p><a href="https://devops.stackexchange.com/questions/3104/how-to-safe-restart-jenkins">How to safe restart Jenkins?</a></p>
  <p>However, all the answers are either HTTP(s) options or using the jenkins-cli.jar CLI route.</p>
  <p>I want to be able to safely restart the Jenkins environment from a script running on the Master host (a Debian Linux system, the script is running from 'cron' as root)</p>
  <p>The Jenkins GUI is using LDAP via the company Active Directory to manage logins, so creating an extra user for this is non trivial (and I don't want to have an extra user open to HTTP Jenkins access if I don't absolutely have to), and using the original default &quot;admin&quot; user isn't possible because Jenkins only allows login from one &quot;security realm&quot;.</p>
  <p>The scripts are able to uncleanly restart Jenkins (because they can restart the processes), but surely it is possible to signal a clean restart somehow.</p>
  <p>One of the answers in the linked question suggests &quot;/etc/init.d/jenkins safeRestart&quot;, but that isn't a valid command.</p>
  
Tags: <jenkins>
Title: How to safe restart Jenkins from OS command line
UserDisplayName: Michael Firth
ThreadText: # How to safe restart Jenkins from OS command line
  
  (Asked by: username_0 on 2021-08-10)
  
  There is an existing question here on how to safely restart Jenkins:
  
  [How to safe restart Jenkins?](https://devops.stackexchange.com/questions/3104/how-to-safe-restart-jenkins)
  
  However, all the answers are either HTTP(s) options or using the jenkins-cli.jar CLI route.
  
  I want to be able to safely restart the Jenkins environment from a script running on the Master host (a Debian Linux system, the script is running from 'cron' as root)
  
  The Jenkins GUI is using LDAP via the company Active Directory to manage logins, so creating an extra user for this is non trivial (and I don't want to have an extra user open to HTTP Jenkins access if I don't absolutely have to), and using the original default "admin" user isn't possible because Jenkins only allows login from one "security realm".
  
  The scripts are able to uncleanly restart Jenkins (because they can restart the processes), but surely it is possible to signal a clean restart somehow.
  
  One of the answers in the linked question suggests "/etc/init.d/jenkins safeRestart", but that isn't a valid command.
  
  **Tags:** jenkins
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-11. Score: 1
  
  Assuming Jenkins is installed via a package which installs a systemd unit file:
  
  ```
  systemctl restart jenkins
  
  ```
  
  > **Comments:**
  > **username_0**: That restarts Jenkins, but it isn't a safe restart. All currently running jobs are aborted in a non-graceful way.  
  > **username_1**: That's not the case for my instance of Jenkins (2.289.3).  If I restart Jenkins with systemd (i.e. the command in my answer), any jobs which were running before the restart are resumed after Jenkins comes back online.  
  > **username_1**: And after the restart, in my build output, I see (for instance) `Resuming build at Mon Aug 16 16:58:58 MDT 2021 after Jenkins restart`.  
  > **username_0**: I don't see how that can be true in the generic case - my jobs are running on "agent" systems via custom build scripts. Jenkins could in theory restart the jobs after an "unclean" restart via systemd, but I don't see how it could resume them. As some of my jobs take 4 hours to run, having it restart it when it is 90% finished wouldn't be great.  
  > **username_1**: Are you running Pipeline jobs, or Freestyle?  Pipeline jobs are robust against Jenkins master restarts, but I don't know about Freestyle jobs.  
  > **username_0**: All my jobs are Freestyle ones.  
  
  ---
  
  