I need to start my own service (SignalR client) written in C# (.NET6) after ttyACM0 port is available. This service should be started after other service (SignalR server).
The server service starts after reboot normally (without any problem). The client service status gives an information:
● RfidHwSigRClient.service
Loaded: loaded (/etc/systemd/system/RfidHwSigRClient.service;
disabled; vendor preset: enabled) Active: inactive (dead) since Mon
2023-03-06 22:20:58 CET; 2min 17s ago Process: 1583
ExecStart=/usr/bin/dotnet
/home/predator/Projects/VSLinuxDbg/RfidHwSigRClient/RfidHwSigRClient.dll
(code=exited, status=0/SUCCESS) Process: 767 ExecStartPre=/bin/sleep
10 (code=exited, status=0/SUCCESS) Main PID: 1583 (code=exited,
status=0/SUCCESS)
Mar 06 22:20:46 PredatorClient systemd[1]: Starting
RfidHwSigRClient.service... Mar 06 22:20:57 PredatorClient
dotnet[1583]: Microsoft.Hosting.Lifetime[0] Application started.
Hosting environment: Production; Content root path: / Mar 06 22:20:57
PredatorClient systemd[1]: Started RfidHwSigRClient.service. Mar 06
22:20:58 PredatorClient dotnet[1583]:
Microsoft.Extensions.Hosting.Internal.Host[9] BackgroundService failed
System.UnauthorizedAccessException: Access to the port '/dev/usb_rfid'
is denied. ---> System.IO.IOException: Device or resource busy ---
End of inner exception stack trace --- at
System.IO.Ports.SafeSerialDeviceHandle.Open(String portName) at
System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate,
Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout,
Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean
rtsEnable, Boolean discardNull, Byte parityReplace) at
System.IO.Ports.SerialPort.Open() at
Predator.Hardware.RfIDCard.RfId.Connect() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\Hardware\RFIDCard\RfId.cs:line
159 at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ConnectAsync() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
95 at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ExecuteAsync(CancellationToken
stoppingToken) in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
77 at
Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService
backgroundService) Mar 06 22:20:58 PredatorClient dotnet[1583]:
Microsoft.Extensions.Hosting.Internal.Host[10] The
HostOptions.BackgroundServiceExceptionBehavior is configured to
StopHost. A BackgroundService has thrown an unhandled exception, and
the IHost instance is stopping. To avoid this behavior, configure this
to Ignore; however the BackgroundService will not be restarted.
System.UnauthorizedAccessException: Access to the port '/dev/usb_rfid'
is denied. ---> System.IO.IOException: Device or resource busy ---
End of inner exception stack trace --- at
System.IO.Ports.SafeSerialDeviceHandle.Open(String portName) at
System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate,
Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout,
Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean
rtsEnable, Boolean discardNull, Byte parityReplace) at
System.IO.Ports.SerialPort.Open() at
Predator.Hardware.RfIDCard.RfId.Connect() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\Hardware\RFIDCard\RfId.cs:line
159 at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ConnectAsync() in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
95 at
Predator.SignalRComm.RfidHwSigRClient.RfidBroker.ExecuteAsync(CancellationToken
stoppingToken) in
D:\Projects\PredatorDotNet\Source\PredatorClientDotNet\SignalRComm\Derived\RfidHwSigRClient\RfidHwSigRClient\RfidBroker.cs:line
77 at
Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService
backgroundService) Mar 06 22:20:58 PredatorClient dotnet[1583]:
Microsoft.Hosting.Lifetime[0] Application is shutting down...
The problem is that the port is not available.
As soon as the Ubuntu is started, I can restart client service (systemctl restart service_name
) from command line, and service works properly (port is ready).
I've read several forums concerning how to start start service.
Unfortunately, without any success.
My udev rules looks like:
KERNEL=="ttyACM*", ATTRS{manufacturer}=="MOD elektronik*", ATTRS{product}=="MOD RFID reader", MODE="0666", SYMLINK+="usb_rfid", ENV{SYSTEMD_WANTS}+="RfidHwSigRClient.service"
My RfidHwSigRClient.service looks like:
[unit]
Description=RFId Card Reader SignalR Client Service
BindsTo=dev-usb_rfid.device
Requires=MainSigRServer.service
After=dev-usb_rfid.device MainSigRserver.service
StartLimitIntervalSec=300
StartLimitBurst=30
[Service]
User=predator
Group=predator
Type=notify
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/dotnet /home/predator/Projects/VSLinuxDbg/RfidHwSigRClient/RfidHwSigRClient.dll
TimeoutStartSec=30s
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-use
I've also check to start service 10sec later (ExecStartPre=/bin/sleep 10
) without any success. User predator belongs to group dialout. Ubuntu is 18.04 LTS.
Please, Does anybody known what I'm doing wrong?
Thank you.
R.