<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-03-21 10:34 GMT+01:00 Colin Guthrie <span dir="ltr"><<a href="mailto:colin@mageia.org" target="_blank">colin@mageia.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Cecil Westerhof wrote on 19/03/16 23:54:<br>
<span class="">> 2016-03-18 17:16 GMT+01:00 Cecil Westerhof <<a href="mailto:cldwesterhof@gmail.com">cldwesterhof@gmail.com</a><br>
</span>> <mailto:<a href="mailto:cldwesterhof@gmail.com">cldwesterhof@gmail.com</a>>>:<br>
<div><div class="h5">><br>
> I wrote as script to put my netbook into suspend when it is locked<br>
> for five minutes and not connected to the AC adapter:<br>
><br>
> <a href="https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/suspend.sh" rel="noreferrer" target="_blank">https://github.com/CecilWesterhof/BashLibrary/blob/master/bin/suspend.sh</a><br>
><br>
> This works perfect when I start it from the command-line, but not<br>
> when I use it as a systemd service. Then I get:<br>
> Failed to start suspend.target: Access denied<br>
> when the<br>
> systemctl suspend || true<br>
> is executed.<br>
><br>
><br>
> For what it is worth: normally ‘systemctl<br>
><br>
> suspend’ returns 1, but from the systemd service it returns 4.<br>
><br>
><br>
><br>
> What could be happening here? And how do I solve it?<br>
><br>
> My service file:<br>
> [Unit]<br>
> Description=Suspend machine when locked and no AC adaptor<br>
><br>
> [Service]<br>
> Type=simple<br>
> ExecStart=/usr/local/bash/bin/suspend.sh<br>
> Restart=always<br>
> User=cecil<br>
><br>
> [Install]<br>
> WantedBy=multi-user.target<br>
> <br>
><br>
> On a related note: why does:<br>
> systemctl suspend<br>
> give an exit status of 1?<br>
> And why does it returns immediately? I had to add a sleep 5 to my<br>
> script to make it work.<br>
<br>
</div></div>You're running the script as the user cecil (User=cecil).<br>
<br>
I suspect the problem is not so much the fact that it's run as this user<br>
(you can probably issue "systemctl suspend" when logged in as the user<br>
right?<br></blockquote><div><br><div class="gmail_default" style="font-family:courier new,monospace;display:inline">The problem was that from the command-line suspend and hibernate could be run, but not from cron, at, or a service. I solved it by using sudo with NOPASSWD in the script.<br></div><br>-- <br></div></div><div class="gmail_signature">Cecil Westerhof</div>
</div></div>