<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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);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">Yes. See my other post where I made the problem smaller. When I just run the script as user cecil from the command-line it works like it should. When the screen is locked for at least five minutes my netbook wil suspend.<br></div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
However when the screen is locked your user session is in a different<br>
state and thus they do not have the same rights and privileges they did<br>
previously when the session is unlocked. They may not be granted the<br>
same permissions as when logged in and thus the permission checks<br>
performed by systemctl when determining if they are allowed to suspend<br>
the machine might fail when locked vs. unlocked.<br></blockquote><div><br><div class="gmail_default" style="font-family:courier new,monospace;display:inline">No, that is not the problem. When I start the script from the command-line it will just put my netbook in suspend when my netbook is locked for at least five minutes.<br></div> <br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
You can test this easily by:<br>
<br>
1. When logged in, run systemctl suspend as cecil in a terminal.<br>
2. Confirm it works!<br>
3. Repeat step 1 but prefix the command with "sleep 15;" and then lock<br>
the screen.<br>
4. Confirm that it breaks.<br>
<br>
This will confirm it's not the fact it's run in a systemd unit that<br>
breaks it, but the fact it's run while the screen is locked.<br>
<br>
If this is to blame then you can write a policy kit rule for the suspend<br>
operation that gives your user more rights.<br>
<br>
That's my first thought (could be wrong!)<br></blockquote><div class="gmail_default" style="font-family:courier new,monospace"><br>From my other post:<br>############################################################<div class="gmail_default" style="font-family:courier new,monospace">When executing<br> systemctl suspend || echo "Error code: ${?}"<br></div><div class="gmail_default" style="font-family:courier new,monospace">from the command-line it outputs<br></div><div class="gmail_default" style="font-family:courier new,monospace"> Error code: 1<br></div><div class="gmail_default" style="font-family:courier new,monospace">and it puts my machine in suspend.<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">When putting it in cron it gives the following errors:<br> Failed to execute operation: Access denied<br> Failed to start suspend.target: Access denied<br>and gives the output:<br> Error code: 4<br><br></div>What is happening here? Is it possible to run 'systemctl suspend' from cron, or is there a reason why this is not possible?<br>############################################################<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">When run as a service or from cron I get an error code of 4 instead of 1 and two times Access denied.<br></div></div><br><div class="gmail_default" style="font-family:courier new,monospace">I also tried at (two minutes I did it from the command-line). This gives also:</div><div class="gmail_default" style="font-family:courier new,monospace"> Failed to execute operation: Access denied<br> Failed to start suspend.target: Access denied<br> Error code: 4<br></div><div class="gmail_default" style="font-family:courier new,monospace">At gives a message about using /bin/sh, but when I run it from /bin/sh it works. An the script I used as a service uses Bash.</div><br>-- <br><div class="gmail_signature">Cecil Westerhof</div>
</div></div>