<!DOCTYPE html>
<html>
  <head>
    <meta http-eqiv="content-type" content="text/html;charset=utf-8">
    <title>Go by Example: Exec'ing Processes</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script type="text/javascript">
    if (window.location.host == "gobyexample.com") {
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-34996217-1']);
      _gaq.push(['_trackPageview']);
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    }
  </script>
  <body>
    <div class="example" id="execing-processes">
      <h2><a href="./">Go by Example</a>: Exec'ing Processes</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>In the previous example we looked at
<a href="spawning-processes">spawning external processes</a>. We
do this when we need an external process accessible to
a running Go process. Sometimes we just want to
completely replace the current Go process with another
(perhaps non-Go) one. To do this we&rsquo;ll use Go&rsquo;s
implementation of the classic
<a href="http://en.wikipedia.org/wiki/Exec_(operating_system)"><code>exec</code></a>
function.</p>

          </td>
          <td class="code empty leading">
	        
            
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
	        <a href="http://play.golang.org/p/neqdJ51KLN"><img title="Run code" src="play.png" class="run" /></a>
            <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;syscall&quot;</span>
<span class="kn">import</span> <span class="s">&quot;os&quot;</span>
<span class="kn">import</span> <span class="s">&quot;os/exec&quot;</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>For our example we&rsquo;ll exec <code>ls</code>. Go requires an
absolute path to the binary we want to execute, so
we&rsquo;ll use <code>exec.LookPath</code> to find it (probably
<code>/bin/ls</code>).</p>

          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre>    <span class="nx">binary</span><span class="p">,</span> <span class="nx">lookErr</span> <span class="o">:=</span> <span class="nx">exec</span><span class="p">.</span><span class="nx">LookPath</span><span class="p">(</span><span class="s">&quot;ls&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nx">lookErr</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
        <span class="nb">panic</span><span class="p">(</span><span class="nx">lookErr</span><span class="p">)</span>
    <span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p><code>Exec</code> requires arguments in slice form (as
apposed to one big string). We&rsquo;ll give <code>ls</code> a few
common arguments. Note that the first argument should
be the program name.</p>

          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre>    <span class="nx">args</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;ls&quot;</span><span class="p">,</span> <span class="s">&quot;-a&quot;</span><span class="p">,</span> <span class="s">&quot;-l&quot;</span><span class="p">,</span> <span class="s">&quot;-h&quot;</span><span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p><code>Exec</code> also needs a set of <a href="environment-variables">environment variables</a>
to use. Here we just provide our current
environment.</p>

          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre>    <span class="nx">env</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Environ</span><span class="p">()</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>Here&rsquo;s the actual <code>syscall.Exec</code> call. If this call is
successful, the execution of our process will end
here and be replaced by the <code>/bin/ls -a -l -h</code>
process. If there is an error we&rsquo;ll get a return
value.</p>

          </td>
          <td class="code">
	        
            <div class="highlight"><pre>    <span class="nx">execErr</span> <span class="o">:=</span> <span class="nx">syscall</span><span class="p">.</span><span class="nx">Exec</span><span class="p">(</span><span class="nx">binary</span><span class="p">,</span> <span class="nx">args</span><span class="p">,</span> <span class="nx">env</span><span class="p">)</span>
    <span class="k">if</span> <span class="nx">execErr</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
        <span class="nb">panic</span><span class="p">(</span><span class="nx">execErr</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>When we run our program it is replaced by <code>ls</code>.</p>

          </td>
          <td class="code leading">
	        
            <div class="highlight"><pre><span class="gp">$</span> go run execing-processes.go
<span class="go">total 16</span>
<span class="go">drwxr-xr-x  4 mark 136B Oct 3 16:29 .</span>
<span class="go">drwxr-xr-x 91 mark 3.0K Oct 3 12:50 ..</span>
<span class="go">-rw-r--r--  1 mark 1.3K Oct 3 16:28 execing-processes.go</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>Note that Go does not offer a classic Unix <code>fork</code>
function. Usually this isn&rsquo;t an issue though, since
starting goroutines, spawning processes, and exec&rsquo;ing
processes covers most use cases for <code>fork</code>.</p>

          </td>
          <td class="code empty">
	        
            
          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        Next example: <a href="signals">Signals</a>.
      </p>
      
      <p class="footer">
        by <a href="https://twitter.com/mmcgrana">@mmcgrana</a> | <a href="mailto:mmcgrana@gmail.com">feedback</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/execing-processes">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
      </p>
    </div>
  </body>
</html>