User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

Automatically logging UDB ASCII messages into a file.

Thu Nov 24, 2022 3:19 pm

Hi
i am getting periodically ASCII messages from an UDP Source on ports 4322 and 4222, which I would like to log into files.
I get ths currently done with opening a Terminal an entering:

Code: Select all

nc -u -l -p 4222 >> /home/pi/events1.log
and opening another Terminal and entering:

Code: Select all

nc -u -l -p 4322 >> /home/pi/events.log
But the sessions must keep open and i can't close the terminals.

How can I let the job be done automatically and without need to keep windows open?

It must not be netcat, any other simple solution is appreciated.

Thank you for helping

Laszlo

MiscBits
Posts: 1098
Joined: Wed Jan 27, 2021 12:48 pm

Re: Automatically logging UDB ASCII messages into a file.

Thu Nov 24, 2022 3:44 pm

Run the job in the background - https://www.linux.com/training-tutorial ... and-linux/

Use TMux / Screen to run multiple terminal sessions?

This first is a fire and forget option, the second makes it easy to end but is more complex.

User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

Re: Automatically logging UDB ASCII messages into a file.

Thu Nov 24, 2022 5:16 pm

Thank you MiscBits.
But the given link gives me no help. (using ampersands?)

Does someone know a better tutorial?

NotRequired
Posts: 339
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Automatically logging UDB ASCII messages into a file.

Thu Nov 24, 2022 6:26 pm

You have multiple terminals (in most Linux based distros, but maybe not in the Raspi OS) in which you can run background stuff, you may switch between them using CTRL+ALT+F1 to F6, the desktop session usually runs in F1: Try to click CTRL+ALT+F2 - this will switch terminal, you return with CTRL+ALT+F1. This would be the cumbersome way of doing it, creating a systemd service which runs on boot and continues to run until shutdown would be the "make and forget" solution:

Create a file named "/etc/systemd/system/logudp.service" and add the following content:

Code: Select all

[Unit]
Description=LogUDP
After=syslog.target network.target

[Service]
Type=simple
User=pi
ExecStart=/usr/bin/sh -c "nc -u -l -p 4222 >> /home/pi/events1.log"

[Install]
WantedBy=multi-user.target

After the file has been created, run "sudo systemctl enable logudp" to enable the service for automatic startup on boot and "sudo systemctl start logudp" to start it immediately. You can make multiple services to run multiple tasks.

EDIT: Added /usr/bin/sh to be sure..
Glowing in the dark!

User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

Re: Automatically logging UDB ASCII messages into a file.

Thu Nov 24, 2022 9:04 pm

Thank you, NotRequired !
You are my champion.
:D

User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

Re: Automatically logging UDB ASCII messages into a file.

Sat Nov 26, 2022 8:39 am

I am a bit confused.
It worked well on my first Pi.

on the second one I did the same:

Code: Select all

sudo nano /etc/systemd/system/logudp.service

[Unit]
Description=LogUDP
After=syslog.target network.target

[Service]
Type=simple
User=pi
ExecStart=/usr/bin/sh -c "nc -u -l -p 4322 >> /home/pi/events.log"

[Install]
WantedBy=multi-user.target
then

Code: Select all

sudo systemctl enable logudp
sudo systemctl start logudp
but upon asking for the status, I get:

Code: Select all

sudo systemctl status logudp
● logudp.service - LogUDP
   Loaded: loaded (/etc/systemd/system/logudp.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2022-11-26 09:22:43 CET; 7min ago
  Process: 4292 ExecStart=/usr/bin/sh -c nc -u -l -p 4322 >> /home/pi/events.log (code=exited, status=203/EXEC)
 Main PID: 4292 (code=exited, status=203/EXEC)

Nov 26 09:22:43 Firholz-004-027 systemd[1]: Started LogUDP.
Nov 26 09:22:43 Firholz-004-027 systemd[1]: logudp.service: Main process exited, code=exited, status=203/EXEC
Nov 26 09:22:43 Firholz-004-027 systemd[1]: logudp.service: Unit entered failed state.
Nov 26 09:22:43 Firholz-004-027 systemd[1]: logudp.service: Failed with result 'exit-code'.
What could have been the cause for the exit?

User avatar
rpdom
Posts: 20858
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Automatically logging UDB ASCII messages into a file.

Sat Nov 26, 2022 9:20 am

Is there anything in the log file /home/pi/events.log?

What happens if you run the command manually?

Code: Select all

/usr/bin/sh -c nc -u -l -p 4322
Unreadable squiggle

NotRequired
Posts: 339
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Automatically logging UDB ASCII messages into a file.

Sat Nov 26, 2022 10:08 am

Code: Select all

status=203/EXEC

^^ Suggests that execution failed because either "/usr/bin/sh" or "nc" is not found. Try to run "which sh" and "which nc" in a terminal to check their presence and location and adapt the service accordingly. You may have success removing the path to "sh" (/usr/bin/) and have the same service work on multiple Pi's.
Glowing in the dark!

User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

Re: Automatically logging UDB ASCII messages into a file.

Sat Nov 26, 2022 10:43 pm

Awarm thank you. It looks logical. I will check tomorrow.

User avatar
rin67630
Posts: 1146
Joined: Fri Mar 04, 2016 10:15 am

[solved!] Automatically logging UDB ASCII messages into a file.

Sun Nov 27, 2022 8:47 pm

Code: Select all

which sh

returned /bin/sh and

Code: Select all

which nc

returned /bin/nc.
So I changed the ExecStart line to:

ExecStart=/bin/sh -c "/bin/nc -u -l -p 4322 >> /home/pi/events.log"

Now it works:

Code: Select all

sudo systemctl status logudp
● logudp.service - LogUDP
   Loaded: loaded (/etc/systemd/system/logudp.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-11-27 21:41:15 CET; 1min 1s ago
 Main PID: 490 (sh)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/logudp.service
           ├─490 /bin/sh -c /bin/nc -u -l -p 4322 >> /home/pi/events.log
           └─494 /bin/nc -u -l -p 4322
Thank you!

NotRequired
Posts: 339
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: Automatically logging UDB ASCII messages into a file.

Mon Nov 28, 2022 10:33 am

You could try to simplify the command to:

Code: Select all

ExecStart=sh -c "nc -u -l -p 4322 >> /home/pi/events.log"
In order to have the same command work on multiple Pi's and OS versions, that would make it more portable.
Glowing in the dark!

Return to “Networking and servers”