Difference between revisions of "Use systemctl from web script on Raspbian"

From PeTechWiki
Jump to navigationJump to search
m
m
Line 21: Line 21:
 
Here the backtick operator is used to execute a shell command, and option -i is used to ignore inhibitors. Make sure you secure access to your script as required.
 
Here the backtick operator is used to execute a shell command, and option -i is used to ignore inhibitors. Make sure you secure access to your script as required.
  
Ref: https://www.freedesktop.org/software/polkit/docs/0.105/pklocalauthority.8.html
+
Reference: https://www.freedesktop.org/software/polkit/docs/0.105/pklocalauthority.8.html
  
 
[[Category:Raspberry Pi]]
 
[[Category:Raspberry Pi]]

Revision as of 12:45, 9 April 2017

On Raspbian jessie, authority to the systemctl command is managed by polkit. To allow a web script to poweroff or reboot your pi using the systemctl command, you'll need to make some configuration changes to polkit-1.

Create or modify /etc/polkit-1/localauthority.conf.d/60-desktop-policy.conf to have the following content:

[Configuration]
AdminIdentities=unix-user:pi;unix-user:www-data;unix-user:0

Create /etc/polkit-1/localauthority/50-local.d/www-data.pkla to have the following content:

[www-data Permissions]                                                                                                                   
Identity=unix-user:www-data                                                                                                              
Action=org.freedesktop.login1.*                                                                                                          
ResultAny=yes                                                                                                                            
ResultInactive=yes                                                                                                                       
ResultActive=yes

You may name this file as you like, but it must have a .pkla extension.

Configuration changes to polkit are applied in real time, so all you need to do now is run your web script from a browser:

<?php
// reboot.php - script to reboot the system
`systemctl reboot -i`;
?>

Here the backtick operator is used to execute a shell command, and option -i is used to ignore inhibitors. Make sure you secure access to your script as required.

Reference: https://www.freedesktop.org/software/polkit/docs/0.105/pklocalauthority.8.html