How to write Linux service application using FreePascal/Lazarus

Linux service application is a type of applications that has no text output in screen, and it runs in background when Linux is working, and it can run in log off mode, also it starts when operating system starts.

To write these type of applications using Lazarus do the following:

1. Create new project, select Program, give it a name, for example TimeLogger

2. In an infinite loop (repeat until false) write current date and time in log.txt text file:

program TimeLogger;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes  , sysutils
{ you can add units after this };

var
TextF: TextFile;
begin
repeat
AssignFile(TextF, 'log.txt');
if FileExists('log.txt') then
Append(TextF)
else
Rewrite(TextF);

Writeln(TextF, DateTimeToStr(Now));

CloseFile(TextF);
Sleep(10000);

until false;
end.

3. Run the application from terminal, after a while stop it using Ctrl + C, to make sure that it is working. You should get log.txt file in the same directory

4. Write script file in /etc/init.d directory and name it timelogger. Put this code in the script:


#!/bin/sh
if [ "$1" = start ] ; then
cd /home/motaz/projects/TimeLogger
/TimeLogger &
fi

change the path of directory in which TimeLogger exists, and give execute permission to this script (sudo chmod +x /etc/init.d/timelogger)

5. Now you can start it using service command:


service timelogger start

And you can check it using ps -ef command:


motaz@t400laptop:~$ ps -ef | grep TimeLogger

you will get a result like this which means that it is working now :

motaz    24561     1  0 17:09 pts/1    00:00:00 ./TimeLogger
motaz    24600 24218  0 17:10 pts/1    00:00:00 grep --color=auto TimeLogger

Don’t forget to check the log file contents (log.txt)

 

6. If you want to check the status and to stop the service, then you need to modify the script source to add these new functionalities:

#!/bin/sh
if [ "$1" = start ] ; then

count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -lt 2 ] ; then
cd /home/motaz/projects/TimeLogger
./TimeLogger &
echo "started"
else
echo "Already running"
fi
fi

if [ "$1" = status ] ; then
count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -lt 2 ] ; then
echo "Stopped"
else
echo "Running..."
fi
fi

if [ "$1" = stop ] ; then
count=$(ps -ef | grep TimeLogger | wc -lc | awk '{print $1}')
if [ $count -gt 1 ] ; then
psid=$(ps -ef | grep TimeLogger | head -n1 | awk '{print $2}')
kill -9 $psid
echo "Stopped"
else
echo "Not Running"
fi
fi

if [ "$1" = restart ]; then
  echo "Restart not implemented, please use stop, then start"
fi

Now can you check it’s status, stop and start it:


service timelogger status

service timelogger stop

service timelogger start

7. Finally to make it work automatically when system is booted, execute this command:


sudo update-rc.d timelogger defaults

and you will get a result like this:


update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/timelogger ...
/etc/rc0.d/K20timelogger -> ../init.d/timelogger
/etc/rc1.d/K20timelogger -> ../init.d/timelogger
/etc/rc6.d/K20timelogger -> ../init.d/timelogger
/etc/rc2.d/S20timelogger -> ../init.d/timelogger
/etc/rc3.d/S20timelogger -> ../init.d/timelogger
/etc/rc4.d/S20timelogger -> ../init.d/timelogger
/etc/rc5.d/S20timelogger -> ../init.d/timelogger

This has been tested on Ubuntu desktop and server version 12.04

5 thoughts on “How to write Linux service application using FreePascal/Lazarus

  1. Salam! Thank you very much! And for service/daemon deveoping there are some prepared classes. For example, internal TDaemon or Brook framework embedded server

Leave a comment