Sign in Join

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

Mailing lists / lazarus / 2019-09

Environment monitoring software on Linux?

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Bo Berglund

I am looking for some advice on how best to approach my project...

I have to create monitoring control software that will run on a Linux computer (Raspberry Pi) and will be responsible for controlling a few measure systems to take readings at regular intervals and saving/forwarding the results.
The system will be unattended, probably housed in a box of some kind outdoors. THere will be an Internet connection through a mobile broadband router in the system.

I have developed instrumentation control software that works to interface with the instrumentation to run the measurements and collect the data afterwards. This is all written in ObjectPascal in Lazarus IDE.
Control of the equipment is done via GPIO connected relays (for power switching and battery control) and RS232 for instrument data interfacing.
I have simplified the software by using the ser2net service on the RPi to be able to use TCP/IP socket connections for "everything" related to system control and data handling.

Now I am at the point where I should create the scheduler running on Linux. About 10-12 years ago I had a similar task and then I was using Delphi and I created a Windows service that would handle the scheduling. But I do not want to mess with Windows anymore so I need to figure out a way to have similar functionality on Linux, i.e. something that will run whenever the computer is started and could check if one or several tasks are due for execution and if so run them.
Is cron the best way or is there some similar function like the Windows services one can easily create? If so is there some sample code in FreePascal for implementing a service?

Any advice welcome.

Bo Berglund Developer in Sweden

Reply by email

Unknown author
to Bo Berglund
Bo Berglund wrote
Is cron the best way or is there some similar function like the Windows services one can easily create?

generally speaking, cron is the equivalent of the winwhatever scheduler...

If so is there some sample code in FreePascal for implementing a service?

why? when you deploy your project, include the needed steps to add the polling schedule to the existing cron on the linux system...

either that or just build a scheduler into (or wrap one around) your project that polls at regular intervals...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.*

Reply by email

Anthony Walter
to Bo Berglund: I am looking for some advice on how best to approach my project... I have to create…
From what you describe it would seem a simple script or program invoked

through cron would be the best way to tackle this problem. Another option is the watch command, which can execute a program every x intervals. And of course if you want to kick off a process when run, you can add a script to init.d or ~.profile. Finally, you also have the option of using ssh and tmux. Tmux allows programs to run between logins, that is connecting and disconnecting to your linux system you can check on a long running program or even an pseudo tty using tmux.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20190926/1759befe/attachment.html>

Reply by email

Bo Berglund
to Unknown author: generally speaking, cron is the equivalent of the winwhatever scheduler... why? when you…
wkitty42--- wrote 15 lines
On 9/26/19 11:12 AM, Bo Berglund via lazarus wrote: > Is cron the best way or is there some similar function like the > Windows services one can easily create? generally speaking, cron is the equivalent of the winwhatever scheduler... > If so is there some sample > code in FreePascal for implementing a service? why? when you deploy your project, include the needed steps to add the polling schedule to the existing cron on the linux system... either that or just build a scheduler into (or wrap one around) your project that polls at regular intervals...

OK, using cron to start the scheduler program is probably the best way on Linux. Once a minute with a quick check for queued tasks and then exit.

I have one problem though, every time the scheduler starts (via cron) it will check if there is a task ready to start at that time. If there is it will engage with the hardware and set it in motion, wait for the measurement to end and then retrieve the data and finish.

If the measure process takes longer than until the next cron invocation due to some extra delays in that particular process, then I don't want the new instance of the scheduler to start another task. The reason is that the hardware is occupied.

Instead I want the new instance to just exit or else go into some wait loop until the first instance quits.
I don't like the use of lock files in order to control this since if the process crashes it will leave behind the lock file and the system blocks.

So is there some way to let an fpc process determine if there is another instance of itself running?
In Windows I used something called Mutex, which was managed by Windows and so could not be left behind...

Bo Berglund Developer in Sweden

Reply by email

Bart
to Bo Berglund
PM Bo Berglund wrote 7 lines
If the measure process takes longer than until the next cron invocation due to some extra delays in that particular process, then I don't want the new instance of the scheduler to start another task. The reason is that the hardware is occupied. Instead I want the new instance to just exit or else go into some wait loop until the first instance quits.

UniqueInstance?
https://wiki.freepascal.org/UniqueInstance

Bart

Reply by email

Bo Berglund
to Bart
Bart wrote 13 lines
On Thu, Sep 26, 2019 at 11:16 PM Bo Berglund via lazarus wrote: > If the measure process takes longer than until the next cron > invocation due to some extra delays in that particular process, then I > don't want the new instance of the scheduler to start another task. > The reason is that the hardware is occupied. > > Instead I want the new instance to just exit or else go into some wait > loop until the first instance quits. UniqueInstance? https://wiki.freepascal.org/UniqueInstance

Hi Bart,
thanks, this seems like a good solution!
I have now tested it on a GUI app by putting it into the lpr file at the very beginning:

uses
....

  uniqueinstanceraw;
{$R *.res}
begin
  if InstanceRunning('myidentifier') then exit;
  Application.Initialize;

Worked straight out of the box!

Now I just have to check that it works also for a console program, which means I have to create two terminals and start it in one and then try to start also in the other.

Bo Berglund Developer in Sweden

Reply by email