title: Start a rust binary as a systemd daemon
body: <p>I'm playing around to find out how to run a rust binary as a systemd daemon in Ubuntu, and haven't been very successful so far.</p>
  <p>What I'm trying to achieve:</p>
  <ul>
  <li>Create a daemon that writes the current time to a file every three seconds.</li>
  </ul>
  <p>I use the <a href="https://crates.io/crates/daemonize" rel="nofollow noreferrer">daemonize</a> crate for this. I am new to rust and have also no experience with daemons. Maybe I'm doing some basic things wrong.</p>
  <pre class="lang-rust prettyprint-override"><code>extern crate daemonize;
  
  use chrono::{DateTime, Utc};
  use daemonize::Daemonize;
  use std::fs::File;
  use std::io::prelude::*;
  use std::{thread, time};
  
  fn main() {
      let args = std::env::args().collect::&lt;Vec&lt;String&gt;&gt;();
      let user = &amp;(*args[1]);
      let group = &amp;(*args[2]);
      let file = &amp;args[3];
  
      let stdout = File::create(&quot;/tmp/daemon.out&quot;).unwrap();
      let stderr = File::create(&quot;/tmp/daemon.err&quot;).unwrap();
  
      let sleep_time = time::Duration::from_millis(3 * 1000);
  
      let mut file = std::fs::OpenOptions::new()
          .append(true)
          .create(true)
          .open(file)
          .unwrap();
  
      let daemonize = Daemonize::new()
          .user(user)
          .group(group)
          .working_directory(&quot;/tmp&quot;) // for default behaviour.
          .stdout(stdout) // Redirect stdout to `/tmp/daemon.out`.
          .stderr(stderr); // Redirect stderr to `/tmp/daemon.err`.
  
      match daemonize.start() {
          Ok(()) =&gt; loop {
              let now: DateTime&lt;Utc&gt; = Utc::now();
              let date = format!(&quot;UTC now is: {}\n&quot;, now);
              file.write_all(date.as_bytes()).unwrap();
              thread::sleep(sleep_time);
          },
          Err(err) =&gt; file
              .write_all(format!(&quot;error: {}&quot;, err).as_bytes())
              .unwrap(),
      };
  }
  
  </code></pre>
  <p>I found a service file on the internet and adjusted it a little ...</p>
  <p>/etc/systemd/system/daemon-test.service</p>
  <pre><code>After=network.target
  StartLimitIntervalSec=0
  [Service]
  Type=simple
  Restart=always
  RestartSec=1
  User=julian
  ExecStart=/home/julian/daemon-test/target/debug/daemon-test julian julian /home/julian/Desktop/daemon.txt
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  <p>i tried to start the service with <code>systemctl start daemon-test</code> but nothing happens. When I run <code>systemctl status daemon-test</code> I get this message:</p>
  <pre><code>systemctl status daemon-test                        
  ● daemon-test.service
       Loaded: loaded (/etc/systemd/system/daemon-test.service; disabled; vendor preset: enabled)
       Active: failed (Result: start-limit-hit) since Sat 2020-07-25 22:53:39 CEST; 31s ago
      Process: 72620 ExecStart=/home/julian/daemon-test/target/debug/daemon-test julian julian /home/julian/Desktop/daemon.txt (code=exited, status=0/SUCCESS)
     Main PID: 72620 (code=exited, status=0/SUCCESS)
  
  Jul 25 22:53:39 jubuntu systemd[1]: daemon-test.service: Scheduled restart job, restart counter is at 5.
  Jul 25 22:53:39 jubuntu systemd[1]: Stopped daemon-test.service.
  Jul 25 22:53:39 jubuntu systemd[1]: daemon-test.service: Start request repeated too quickly.
  Jul 25 22:53:39 jubuntu systemd[1]: daemon-test.service: Failed with result 'start-limit-hit'.
  Jul 25 22:53:39 jubuntu systemd[1]: Failed to start daemon-test.service.
  Jul 25 22:54:10 jubuntu systemd[1]: /etc/systemd/system/daemon-test.service:1: Assignment outside of section. Ignoring.
  Jul 25 22:54:10 jubuntu systemd[1]: /etc/systemd/system/daemon-test.service:2: Assignment outside of section. Ignoring.
  
  </code></pre>