Recent

Author Topic: command as root??  (Read 4891 times)

hamza

  • Jr. Member
  • **
  • Posts: 52
command as root??
« on: May 20, 2015, 12:35:11 am »
hi all
I am trying to active the digital output using bash script,the bash script is working correctly From command terminal, but wehn I execute it using lazarus , a new error a ppear "
/home/pi/confi_outp.sh  ---echo "out" > sys/class/gpio/gpio4/direction    permission denied"

so, how can I execute the command as root?? or how can solve this problem???

plz see below code

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, process, FileUtil, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Buttons, ExtCtrls,Unix;


type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    config_out: TProcess;

      private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  x,y,z, m1,m2,m3:integer ;

implementation

{$R *.lfm}

procedure configer_outputs(m1:integer);
var
Filename1: String;
config_out: TProcess;
begin
  config_out:=TProcess.Create(nil);
  Filename1:='/home/pi/confi_outp.sh';
  try
    config_out.CommandLine:=Filename1;
    config_out.Execute;
  finally
    config_out.Free;
  end;
  end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  configer_outputs(m1);// configer outputs
 
end;
end.

also the script file is
#!/bin/bash

#clear gpio 4 as output
sudo echo "4" > /sys/class/gpio/unexport
#set gpio 4 as output
sudo echo "4" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio4/direction
sudo echo "1" > /sys/class/gpio/gpio4/value
             

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: command as root??
« Reply #1 on: May 20, 2015, 12:52:06 am »
Run your project as a root. (su, sudo, kdesu, kdesudo etc.)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: command as root??
« Reply #2 on: May 20, 2015, 02:19:55 am »
You could try to use encapsulated shell calls with "sudo", e.g.:

Code: [Select]
var
  gReturnCode: longint; {stores the result of the IO operation}

...
gReturnCode := fpsystem('sudo echo "17" > /sys/class/gpio/export');
...
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux


Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: command as root??
« Reply #4 on: May 20, 2015, 12:34:46 pm »
Another possibility, if you want to run just one specific command, would be adding it to sudoers (/etc/sudoers).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: command as root??
« Reply #5 on: May 20, 2015, 01:41:30 pm »
if you give permission for your user to write on sys/class/gpio/gpio4/direction  ?

hamza

  • Jr. Member
  • **
  • Posts: 52
Re: command as root??
« Reply #6 on: May 20, 2015, 06:37:54 pm »
thank you all,  its woring now with adding sudo :D :D

 

TinyPortal © 2005-2018