title: How do I restart nginx only after the configuration test was successful on Ubuntu?
body: <p>When I restart the nginx service on a command line on an Ubuntu server, the service crashes when a nginx configuration file has errors. On a multi-site server this puts down all the sites, even the ones without configuration errors.</p>
  
  <p>To prevent this, I run the nginx configuration test first:</p>
  
  <pre><code>nginx -t
  </code></pre>
  
  <p>After the test ran successful, I could restart the service:</p>
  
  <pre><code>/etc/init.d/nginx restart
  </code></pre>
  
  <p><strong>Or</strong> only reload the nignx site configs without a restart:</p>
  
  <pre><code>nginx -s reload
  </code></pre>
  
  <p>Is there a way to combine those two commands where the restart command is conditional to the configuration test's result?</p>
  
  <p>I couldn't find this online and <a href="http://wiki.nginx.org/CommandLine" rel="noreferrer">the official documentation</a> on this is rather basic. I don't know my way around Linux that well, so I don't know if what I'm looking for is right in front of me or not possible at all.</p>
  
  <p>I'm using nginx v1.1.19.</p>