Recent

Author Topic: Service for Raspberry Pi  (Read 13991 times)

nhatdung

  • New Member
  • *
  • Posts: 37
Service for Raspberry Pi
« on: April 07, 2016, 09:45:14 am »
Hi!
I'm trying write a daemon service for raspberry pi use lazarus, follow the tutorial but the service cannot start.
I follow this
http://wiki.freepascal.org/Daemons_and_Services#Linux_.28only_for_older_Debian.29

any1 known how to make the service work ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16184
  • Censorship about opinions does not belong here.
Re: Service for Raspberry Pi
« Reply #1 on: April 07, 2016, 10:21:33 am »
Don't use the older example, but the example for fedora etc.

There is a recent thread here that shows you how. Raspbian also moved to Jessie, which means it uses systemd and not init.d. You may have to copy a part of the init.d configuration, though. I will see if I can edit the wiki to be more current. The examples are not very clear and outdated. But the above should help....

If you can't figure it out yourself, plz report back. I have several Pi's running fpc daemons and my Pi3 is due to arrive today <JAJ!>
« Last Edit: April 07, 2016, 10:24:36 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

nhatdung

  • New Member
  • *
  • Posts: 37
Re: Service for Raspberry Pi
« Reply #2 on: April 08, 2016, 05:04:15 am »
YAH, i also use jessie, let me try write 1 and see if can install it or not

nhatdung

  • New Member
  • *
  • Posts: 37
Re: Service for Raspberry Pi
« Reply #3 on: April 08, 2016, 06:41:56 am »
i cannot make it work
lazarus 1.2.4
seem i can install it correctly, i follow this tutorial

http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/

my code is really simple
Code: Pascal  [Select][+][-]
  1. unit DaemonUnit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, DaemonApp;
  9.  
  10. type
  11.  
  12.   { TDaemon1 }
  13.  
  14.   TDaemon1 = class(TDaemon)
  15.     procedure DataModuleCreate(Sender: TObject);
  16.     procedure DataModuleDestroy(Sender: TObject);
  17.     procedure DataModuleExecute(Sender: TCustomDaemon);
  18.     procedure DataModuleShutDown(Sender: TCustomDaemon);
  19.     procedure DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  20.     procedure DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  21.     procedure Timer1Timer(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28.   { TMyThread }
  29.  
  30.   TMyThread = class(TThread)
  31.   protected
  32.     procedure Execute; override;
  33.   end;
  34.  
  35. var
  36.   Daemon1: TDaemon1;
  37.  
  38.  
  39. implementation
  40.  
  41. procedure RegisterDaemon;
  42. begin
  43.   RegisterDaemonClass(TDaemon1)
  44. end;
  45.  
  46. {$R *.lfm}
  47.  
  48. { TDaemon1 }
  49. procedure add_to_file(s: ansistring; FileName: string = 'log.log';
  50.   NewLine: boolean = True);
  51. var
  52.   f: TFileStream;
  53. begin
  54.   try
  55.     if FileExists(FileName) then
  56.     begin
  57.       f := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyNone);
  58.       f.Seek(0, soEnd);
  59.     end
  60.     else
  61.       f := TFileStream.Create(FileName, fmCreate or fmShareDenyNone);
  62.     if NewLine then
  63.       s := s + #$D#$A;
  64.     f.Write(PChar(s)^, Length(s));
  65.     f.Free;
  66.   except
  67.   end;
  68. end;
  69.  
  70. { TMyThread }
  71.  
  72. procedure TMyThread.Execute;
  73. var s : string;
  74. begin
  75.   sleep(10000);
  76.   s := 'start delay @ ' + DateTimeToStr(Now);
  77.   add_to_file(s, '/home/pi/dung.txt');
  78. end;
  79.  
  80. procedure TDaemon1.DataModuleCreate(Sender: TObject);
  81. begin
  82. end;
  83.  
  84. procedure TDaemon1.DataModuleDestroy(Sender: TObject);
  85. begin
  86. end;
  87.  
  88. procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon);
  89. begin
  90. end;
  91.  
  92. procedure TDaemon1.DataModuleShutDown(Sender: TCustomDaemon);
  93. begin
  94.  
  95. end;
  96.  
  97. procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: Boolean);
  98. var s : string;
  99. begin
  100.   s := 'start at ' + DateTimeToStr(Now);
  101.   add_to_file(s, '/home/pi/dung.txt');
  102.  
  103.   TMyThread.Create(False);
  104. end;
  105.  
  106. procedure TDaemon1.DataModuleStop(Sender: TCustomDaemon; var OK: Boolean);
  107. var s : string;
  108. begin
  109.   s := 'finis at ' + DateTimeToStr(Now);
  110.   add_to_file(s, '/home/pi/dung.txt');
  111. end;
  112.  
  113. procedure TDaemon1.Timer1Timer(Sender: TObject);
  114. var s : string;
  115. begin
  116.   s := DateTimeToStr(Now);
  117.   add_to_file(s, '/home/pi/dung.txt');
  118. end;
  119.  
  120.  
  121. initialization
  122.   RegisterDaemon;
  123. end.
  124.  
whole data here:
https://www.dropbox.com/s/me1bcpbhr0wnuxb/testdaemon.zip?dl=0

trying install follow those tutorial seem it working but dunno why the script write to file not run
Quote

[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=forking
ExecStart=/home/pi/Projects/testdaemon/testdm

[Install]
WantedBy=multi-user.target




Quote
pi@raspberrypi:~/Projects $ sudo systemctl status dung.service
● dung.service - My Script Service
   Loaded: loaded (/lib/systemd/system/dung.service; enabled)
   Active: inactive (dead) since Fri 2016-04-08 03:43:42 UTC; 8min ago
  Process: 818 ExecStart=/home/pi/Projects/testdaemon/testdm (code=exited, statu                                                                                        s=0/SUCCESS)

Apr 08 03:43:42 raspberrypi testdm[818]: Usage: /home/pi/Projects/testdaemon...]
Apr 08 03:43:42 raspberrypi testdm[818]: Where command is one of the following:
Apr 08 03:43:42 raspberrypi testdm[818]: -i --install   To install the progr...e
Apr 08 03:43:42 raspberrypi testdm[818]: -u --uninstall To uninstall the service
Apr 08 03:43:42 raspberrypi testdm[818]: -r --run       To run the service
Apr 08 03:43:42 raspberrypi systemd[1]: Started My Script Service.
Hint: Some lines were ellipsized, use -l to show in full.
« Last Edit: April 08, 2016, 07:20:40 am by nhatdung »

nhatdung

  • New Member
  • *
  • Posts: 37
Re: Service for Raspberry Pi
« Reply #4 on: April 14, 2016, 01:34:21 pm »
still cannot make it work, anyone ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16184
  • Censorship about opinions does not belong here.
Re: Service for Raspberry Pi
« Reply #5 on: April 14, 2016, 01:47:04 pm »
Plz, if you do what I wrote it will work....
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018