[Linux] Program that runs in the background lazarus / 61-Beginners ---------------------------------------------------------------------- abdoun.brahim · 2022-05-11 16:21 Hello I want to create a Lazarus Linux program in console mode that will run in the background (without seeing the result on the screen): it is a program that will run in the background and that will play on the serial port; the traces of communications will be recorded in a log file and the data captured in a PostgreSQL database. Thank you for your help. ---------------------------------------------------------------------- winni · 2022-05-11 16:54 Hi! What you want is called a daemon Read this: https://wiki.lazarus.freepascal.org/Daemons_and_Services Winni ---------------------------------------------------------------------- abdoun.brahim · 2022-05-11 17:09 Hello Thank you very much I will look at the links. Thank you ---------------------------------------------------------------------- MarkMLl · 2022-05-11 20:43 In addition to what Winni's said, I'd remind you that Lazarus can be used to develop a non-GUI program that can simply be started with its standard handles defined as /dev/null i.e. like Code: [Select]./something >/dev/null 2>&1 & MarkMLl ---------------------------------------------------------------------- dbannon · 2022-05-12 02:49 Quote from: MarkMLl on May 11, 2022, 10:43:25 pmCode: [Select]./something >/dev/null 2>&1 & Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i nohup ./something Is my preferred means, it disconnects the current terminal so closing that terminal does not shut down the app, it redirects std out to a file called 'nohup'. For all intents and purposes, its a daemon. But you could also look at https://wiki.freepascal.org/Daemons_and_Services for a more structured approach. Davo ---------------------------------------------------------------------- Warfley · 2022-05-12 08:31 I generally advise against using a LazDaemon application on Linux, because it is designed around the Windows API structure for services, which is rather complicated. Like this whole putting your functionatlity in a worker thread that is then signalised through some event methods comes from the way the Windows handles Services. When you are not targeting Windows, this is all overhead that you do not need, because Daemons on Linux are really easy, because they are just console applications (in fact on Linux every application is a console application, the idea that there are different types of applications is a Windows only concepts). So to create a daemon for linux is really easy: Step 1: Create and debug your application as console application Step 2: Run it in the background To run an application in the background a few things where already mentioned. Here is some further information. To run an application from your shell in the background, you need to add an & after the command: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication &There you have two Problems, the first one is that your console output is still bound to the terminal you started your program in, so if you close it and your program tries to do a WriteLn, it will get a broken pipe error, and this probably crashes your application. So you need to handle the output. If you want to log the output, you can just write it into a file: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication > logfile.txt 2>&1 &The 2>&1 pipes the stderr over the same output as stdout, if you want different logs: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication > output.log 2> error.log &If you want to have it a bit nicer with timestamps, you can also add them through bash: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication 2>&1 | ts > log.txt &but this limits a bit your abilities (e.g. creating a seperate log for stderr, or taking the PID, which I will come to later), so maybe its better to timestamp within your application, e.g. by using a logging library You can also completely ignore the output by piping it to /dev/null Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication > /dev/null 2>&1 & The next problem is that when the shell that opend your program closes, your progrem gets the HUP signal sent, the default action is to close the program. There are two options to avoid this, either start your program via nohup, which basically just catches this signal and ignores it, and everything else is just passed through to your application: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> nohup ./myapplication > log.txt 2>&1 & This is very basic to start a program in the background from your shell. You might want to find that program again later, in which case you might want to save the pid: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication > /dev/null 2>&1 &$> PID=$!which you can then store locally, e.g. to kill the process later: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $> ./myapplication > /dev/null 2>&1 &$> PID=$!$> echo $PID > myapp.pid# Killing the process later$> PID=$( kill $PIDSo you could just create two simple bash scripts, runService.sh: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/bin/bashset -enohup ./myapplication > output.log 2> error.log &echo $! > myapplication.pidand stopService.sh: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/bin/bashset -ePID=$( output.log 2> error.log &echo $! > myapplication.pidand stopService.sh: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #!/bin/bashset -eif [ ! -f myapplication.pid ]; then  echo Service is not running  exitfiPID=$(0 then begin     exit;  end;repeatAssignFile(TextF, 'log.txt');if FileExists('log.txt') thenAppend(TextF)elseRewrite(TextF); Writeln(TextF, DateTimeToStr(Now)); CloseFile(TextF);Sleep(10000); until false;except   on e:exception do   Writeln(TextF, DateTimeToStr(Now)+': erreur:'+e.message);end;end. ---------------------------------------------------------------------- dbannon · 2022-05-13 04:12 yes Abdoun, that will work but if you read the responses you see here, you would realise that most of it is unnecessary. And unnecessary code is bad code. Without testing - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program TimeLogger;{$APPTYPE CONSOLE}{$mode objfpc}{$H+} uses{$IFDEF UNIX}{$IFDEF UseCThreads}cthreads,{$ENDIF}{$ENDIF}Classes  , sysutils ,baseunix{ you can add units after this }; begin    repeat        Writeln(DateTimeToStr(Now));        Sleep(10000);    until false;end. Start it with nohup command and you will find your datestamp lines in the file 'nohup' or read Warfley's means of directing it to a specific file. D ---------------------------------------------------------------------- Warfley · 2022-05-13 07:32 It's not just that this is uneccessary code, it also makes debugging of this application impossible. You are just going to make your life harder with this. Just create a normal command line application that is easy to create and debug, and then execute it in the background as described above ---------------------------------------------------------------------- MarkMLl · 2022-05-13 07:52 Quote from: Warfley on May 13, 2022, 09:32:26 amIt's not just that this is uneccessary code, it also makes debugging of this application impossible. You are just going to make your life harder with this. Just create a normal command line application that is easy to create and debug, and then execute it in the background as described above I strongly agree, but it's worth noting that the fork() technique is a transcription of legacy C code from some unspecified source (I hope it wasn't the FPC wiki). The result is, by absolutely any standards, an unacceptable hodgepodge: on the one hand it uses traditional C/unix "lean and mean" fork(), on the other it introduces exception handling and a textfile which are very much "non-C". MarkMLl ---------------------------------------------------------------------- abdoun.brahim · 2022-05-13 17:21 Thank you very much for your help it's true it's useless to use fpfork I will use : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i nohup ./myapplication > output.log 2> error.log & Thank you