AXIF
Posts: 29
Joined: Wed Sep 23, 2020 12:35 pm

How to debug a daemon service in Raspberry pi

Fri Jun 18, 2021 7:37 am

Hi Guys,

With my raspi zero W, some sensors are connecetd (rain sensor, bm280). It is installed outside in the field. Raspi is collecting the data via a python script and sending it to my remote server with mqtt protocol by using a Huawei surfing stick. It works fine, but some times for unkwon reason it stops sending data. There could be following problems.

1. Either Huawei sticks stops working
2. Or the problem with I2C interface of bme280

I have created a daemon with the systemd so that python script could start at each boot up. So, now my question is, how can and where can i look to know this problem. Sometimes i look at syslog and sometime on daemon.log but dont know where the problem lies.

Thank you for your support

RonR
Posts: 2384
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: How to debug a daemon service in Raspberry pi

Fri Jun 18, 2021 7:58 am

Simply add some lines to your program that output messages or values at various points to your own log file(s) (for example: write "Start of poll sensors loop" to /home/pi/mylog) . With a few of these, you can determine where your program is getting (or not getting) to or stops/hangs. Once you find the general area of failure, you can add additional instrumentation messages in that area to isolate the issue further.

AXIF
Posts: 29
Joined: Wed Sep 23, 2020 12:35 pm

Re: How to debug a daemon service in Raspberry pi

Sat Jun 19, 2021 9:33 am

RonR wrote:
Fri Jun 18, 2021 7:58 am
Simply add some lines to your program that output messages or values at various points to your own log file(s) (for example: write "Start of poll sensors loop" to /home/pi/mylog) . With a few of these, you can determine where your program is getting (or not getting) to or stops/hangs. Once you find the general area of failure, you can add additional instrumentation messages in that area to isolate the issue further.

Thank you very much ! Could you please explain a bit more if possible with simple code 2 , 3 lines.

lurk101
Posts: 902
Joined: Mon Jan 27, 2020 2:35 pm
Location: Cumming, GA (US)

Re: How to debug a daemon service in Raspberry pi

Sat Jun 19, 2021 4:08 pm

AXIF wrote:
Sat Jun 19, 2021 9:33 am
RonR wrote:
Fri Jun 18, 2021 7:58 am
Simply add some lines to your program that output messages or values at various points to your own log file(s) (for example: write "Start of poll sensors loop" to /home/pi/mylog) . With a few of these, you can determine where your program is getting (or not getting) to or stops/hangs. Once you find the general area of failure, you can add additional instrumentation messages in that area to isolate the issue further.

Thank you very much ! Could you please explain a bit more if possible with simple code 2 , 3 lines.
Run your daemon from the command line with sudo specifying any necessary parameters.

Then monitor the logged output with

Code: Select all

tail -f /var/log/daemon.log
Modern daemons use stdout and stderr to log output, letting systemd reroute it to the log files. In either case you can just use printf, or even gdb for debugging.

W. H. Heydt
Posts: 14968
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: How to debug a daemon service in Raspberry pi

Sat Jun 19, 2021 4:53 pm

To add to what has been said...

A daemon is just a program. Usually it is run in the background (that is, not connected to a terminal session), and often started automatically (systemd in your case).

So...you debug it just like you would any other program. You can add your on logging to it. You can run it as a foreground process in its own terminal window. And so on, just like you'd do with any other program you were trying to debug.

AXIF
Posts: 29
Joined: Wed Sep 23, 2020 12:35 pm

Re: How to debug a daemon service in Raspberry pi

Mon Jun 21, 2021 4:30 pm

Thank you very much guys! For your timely support

I have debugged it with ( tail -f /home/pi/var/log/daemon.log). I was able to find my error. But even now I could not understand what kind of stuff is going on in daemon log. I think in the daemon log all the daemon script log is passing on. I hope i would understand it

Return to “General discussion”