Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-06

Daemon doesn't work right at boottime.

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

A.J. Venter

Hi all,
I have written a daemon as part of a project (I used the daemon.pp example as a skeleton), it's working quite perfectly if you start it from the commandline, including from the bootscript I wrote.

But when started at boottime it doesn't operate correctly anymore.

The timer still counts perfectly but it is as if it never hits the trigger times. By following the log files I can verify that the timer-triggered events never occur if it is events, and their associated intervals are read from plugin configuration files in /etc/
All I can imagine is that for some reason when started from the bootscripts - the daemon suddenly doesn't find the files... but why would it then find it if you run the same script from a terminal later ?

Any idea where I should start looking ?

A.J.

"there's nothing as inspirational for a hacker as a cat obscuring a bug by sitting in front of the monitor" - Boudewijn Rempt A.J. Venter Chief Software Architect OpenLab International www.getopenlab.com www.silentcoder.co.za +27 82 726 5103
Burkhard Carstens
to A.J. Venter
A.J. Venter wrote 18 lines
Hi all, I have written a daemon as part of a project (I used the daemon.pp example as a skeleton), it's working quite perfectly if you start it from the commandline, including from the bootscript I wrote. But when started at boottime it doesn't operate correctly anymore. The timer still counts perfectly but it is as if it never hits the trigger times. By following the log files I can verify that the timer-triggered events never occur if it is started at boottime. These events, and their associated intervals are read from plugin configuration files in /etc/ All I can imagine is that for some reason when started from the bootscripts - the daemon suddenly doesn't find the files... but why would it then find it if you run the same script from a terminal later ? Any idea where I should start looking ?

Just a shot into the dark: maybe you start yout deamon, before your local timezone is set up?

Does it change anything, when you move your bootscript to a higher position in rc.x, e.g. make it S99whatever?

Burkhard

A.J. Venter
to Burkhard Carstens
Show quoted text 7 lines
> Any idea where I should start looking ? Just a shot into the dark: maybe you start yout deamon, before your local timezone is set up? Does it change anything, when you move your bootscript to a higher position in rc.x, e.g. make it S99whatever?

Good theory, but my daemon is very nearly the last bootscript to run, quite a long while after this system at least has set up the local timezone, ran ntpdate once and then started ntpd.

The log on a normal run shows each plugin getting run on zero, then again on whatever their interval dictates - but from bootup the only thing that shows up in the log are the tick's

Here are the critical parts of the source:

<snip>
Procedure ReadPlugins;
Var Plugins, ThisPlugin : TstringList;
            I,J : Integer;
	CMD,Min : String;
Begin
Writeln('Woled started');
Assign(fLog,logname);
Append(Flog);
Writeln(Flog,'Woled started');
Actions := TStringList.Create;
ThisPLugin := TstringList.Create;
GetDirList('/etc/wole/plugins/*',Plugins);
If Plugins.Count = 0 then
Begin
	Writeln(Flog,'No plugins configured, exciting');
	close(Flog);
	halt;
end;
For I := 0 to Plugins.Count -1 do
Begin
   ThisPlugin.Clear;
   ThisPlugin.LoadFromFile('/etc/wole/plugins/'+Plugins[I]);
   for J := 0 to ThisPlugin.Count -1 do
   Begin
	If pos('ACTION=',uppercase(ThisPlugin[J]))  0 then
	begin
	 CMD:=Copy(ThisPlugin[J],pos('=',ThisPlugin[J])+1,length(ThisPlugin[J]));
	end;
	If pos('INTERVAL=',uppercase(ThisPlugin[J]))  0 then
	begin
	 Min:=Copy(ThisPlugin[J],pos('=',ThisPlugin[J])+1,length(ThisPlugin[J]));
	end;
   end;
   If (Length(Min)  0) And (Length(CMD)  0) then
   begin
	Actions.Add(min+'='+cmd);
   end else
   Begin
	Writeln(Flog,'Could not read ',Plugins[I],' format error.');
   end;
end;
Close(FloG);
ThisPlugin.Free;
Plugins.Free;
Writeln('Handling ',actions.count,' plugins');
end;
<snip>
begin{MAIN}
<snip>
 {Inside the fork}
   Repeat
      If bHup Then Begin
         {$I-}
         Close(fLog);
         {$I+}
         IOResult;
         NewLog;
         bHup := false;
      End;
      {----------------------}
      Assign(Flog,Logname);
      Append(Flog);
      Writeln (Flog,'Tick - ',minutes,' minutes');
      For PCount := 0 to Actions.Count -1 do
      Begin
	Mint := StrToInt(Copy(Actions[Pcount],1,pos('=',Actions[Pcount]) -1));
	If Minutes mod Mint = 0 then
	Begin
	Writeln(Flog,'Executing plugin action:');
Writeln(Flog,'	',copy(Actions[Pcount],pos('=',Actions[Pcount])+1,length(Actions[Pcount])));
shell(copy(Actions[Pcount],pos('=',Actions[Pcount])+1,length(Actions[Pcount])));
	Writeln(Flog,'Action completed, resuming counter');
	end;
      end;
      Close(fLog);
      If bTerm Then
         BREAK
      Else
         { wait a while }
         fpSelect(0,nil,nil,nil,60000);
	 inc(Minutes);
   Until bTerm;
   Actions.Free;
"there's nothing as inspirational for a hacker as a cat obscuring a bug by sitting in front of the monitor" - Boudewijn Rempt A.J. Venter Chief Software Architect OpenLab International www.getopenlab.com www.silentcoder.co.za +27 82 726 5103
Joep Blom
to A.J. Venter
A.J. Venter wrote 112 lines
>>Any idea where I should start looking ? >> >> >Just a shot into the dark: maybe you start yout deamon, before your >local timezone is set up? > >Does it change anything, when you move your bootscript to a higher >position in rc.x, e.g. make it S99whatever? > > Good theory, but my daemon is very nearly the last bootscript to run, quite a long while after this system at least has set up the local timezone, ran ntpdate once and then started ntpd. The log on a normal run shows each plugin getting run on zero, then again on whatever their interval dictates - but from bootup the only thing that shows up in the log are the tick's Here are the critical parts of the source: <snip> Procedure ReadPlugins; Var Plugins, ThisPlugin : TstringList; I,J : Integer; CMD,Min : String; Begin Writeln('Woled started'); Assign(fLog,logname); Append(Flog); Writeln(Flog,'Woled started'); Actions := TStringList.Create; ThisPLugin := TstringList.Create; GetDirList('/etc/wole/plugins/*',Plugins); If Plugins.Count = 0 then Begin Writeln(Flog,'No plugins configured, exciting'); close(Flog); halt; end; For I := 0 to Plugins.Count -1 do Begin ThisPlugin.Clear; ThisPlugin.LoadFromFile('/etc/wole/plugins/'+Plugins[I]); for J := 0 to ThisPlugin.Count -1 do Begin If pos('ACTION=',uppercase(ThisPlugin[J])) 0 then begin CMD:=Copy(ThisPlugin[J],pos('=',ThisPlugin[J])+1,length(ThisPlugin[J])); end; If pos('INTERVAL=',uppercase(ThisPlugin[J])) 0 then begin Min:=Copy(ThisPlugin[J],pos('=',ThisPlugin[J])+1,length(ThisPlugin[J])); end; end; If (Length(Min) 0) And (Length(CMD) 0) then begin Actions.Add(min+'='+cmd); end else Begin Writeln(Flog,'Could not read ',Plugins[I],' format error.'); end; end; Close(FloG); ThisPlugin.Free; Plugins.Free; Writeln('Handling ',actions.count,' plugins'); end; <snip> begin{MAIN} <snip> {Inside the fork} Repeat If bHup Then Begin {$I-} Close(fLog); {$I+} IOResult; NewLog; bHup := false; End; {----------------------} Assign(Flog,Logname); Append(Flog); Writeln (Flog,'Tick - ',minutes,' minutes'); For PCount := 0 to Actions.Count -1 do Begin Mint := StrToInt(Copy(Actions[Pcount],1,pos('=',Actions[Pcount]) -1)); If Minutes mod Mint = 0 then Begin Writeln(Flog,'Executing plugin action:'); Writeln(Flog,' ',copy(Actions[Pcount],pos('=',Actions[Pcount])+1,length(Actions[Pcount]))); shell(copy(Actions[Pcount],pos('=',Actions[Pcount])+1,length(Actions[Pcount]))); Writeln(Flog,'Action completed, resuming counter'); end; end; Close(fLog); If bTerm Then BREAK Else { wait a while } fpSelect(0,nil,nil,nil,60000); inc(Minutes); Until bTerm; Actions.Free;

Just my 2 cents: If you're on linux look at the file rw permissions, especially for I/O-interfaces like ttyS0, ttyS1, etc. In Fedora they have only root write permissions.
Joep

A.J. Venter
to Joep Blom
Just my 2 cents: If you're on linux look at the file rw permissions, especially for I/O-interfaces like ttyS0, ttyS1, etc. In Fedora they have only root write permissions. Joep

Well as far as I know I'm not writing to anything except the log file, and the daemon is running as root anyway.

/me considers recoding the whole thing as a cronjob... (A joke, in reality I simply don't have the diskspace to put cron where this is needed so it's not an option even if it COULD do the work).

A.J.

"there's nothing as inspirational for a hacker as a cat obscuring a bug by sitting in front of the monitor" - Boudewijn Rempt A.J. Venter Chief Software Architect OpenLab International www.getopenlab.com www.silentcoder.co.za +27 82 726 5103
Geno Roupsky
to A.J. Venter

Well one difference between running the init script from shell and during boot is the environment. Are you using some shell commands or something else for let say GetDirList, because during boot the path variable is not initialized as it is when you are in shell (actually I think it is empty) and if you don't specify absolute paths or manually fill it the required programs could not be located.

Also look for something else from the environment that your program depends on.

Try to clear the environment in the shell and try to run the init script and see what will happen.

Geno Roupsky
A.J. Venter
to A.J. Venter: Well as far as I know I'm not writing to anything except the log file, and the daemon is…
/me considers recoding the whole thing as a cronjob... (A joke, in reality I simply don't have the diskspace to put cron where this is needed so it's not an option even if it COULD do the work).

Interesting, I have since learned something else.
In fact it DOES work - but there is an odd difference. When started from the commandline, the timer starts at 0, ergo my maths (using mod to determine interval matches) means that every plugin gets run at least once on startup.
For some reason however, when started at boottime - the timer starts at 1 - so things don't run until the first match of their interval setting. I actually wanted things to be launched once at boot-up - and then only again at the next interval each time.
I do think this is a very odd thing to find though - why would the counter start with different values depending on whether it is started by a script or by hand ?
Any ideas ?

"there's nothing as inspirational for a hacker as a cat obscuring a bug by sitting in front of the monitor" - Boudewijn Rempt A.J. Venter Chief Software Architect OpenLab International www.getopenlab.com www.silentcoder.co.za +27 82 726 5103