<p>我想我找到问题所在了。。。<code>systemd</code>默认情况下作为<code>root</code>运行其服务。我认为(见下文),这将/应该允许为另一个用户或cli上的实际用户使用sudo来运行类似于<code>root</code>的东西。你知道吗</p>
<p>只关注systemd的服务文件,我在<a href="http://man7.org/linux/man-pages/man5/systemd.exec.5.html" rel="nofollow noreferrer">man pages</a>中发现有一个<code>[Service]</code>参数,允许您指定谁运行服务。在本例中,我尝试将参数设置为:</p>
<pre><code>[Service]
User=pi
ExecStart=/home/pi/my_program.sh
</code></pre>
<p>这允许服务在没有错误(上面提到的错误)的情况下运行。你知道吗</p>
<p>我也可以使用这些设置,执行时不会出错:</p>
<pre><code>[Service]
User=root
ExecStart=/home/pi/my_program.sh
</code></pre>
<p><strong>原因:</strong></p>
<p>对于在这里找到自己的其他人,如果从<code>[Service]</code>部分省略<code>User=</code>参数,则默认值为<code>root</code>。但是,这与将<code>User=</code>参数设置为<code>root</code>(例如,<code>User=root</code>)或另一个用户(在我的例子中是pi)不同。你知道吗</p>
<p>我不太明白其中的细微差别,但是从<a href="https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Environment%20variables%20in%20spawned%20processes" rel="nofollow noreferrer">environmental variables</a>的文档来看,<code>User=</code>参数用于设置<code>$PATH</code>变量。如果服务文件没有<code>User=</code>集,我认为这会导致路径分配出现问题。我认为这就是为什么将<code>User=</code>设置为pi或root可以解决这个问题,而使用默认值似乎会导致这个问题。你知道吗</p>
<p>欢迎讲解和教育。谢谢。你知道吗</p>