0

Issue:

I am running a .net core application on a Raspberry PI. I am loading the configuration from a JSON file and storing in a class for reference in the application - a hosted service in this case. If I start the application manually and then update the config file, the changes are reflected in the application. If however I set the application to launch automatically on boot and then modify the config file, the application does not use the updated config.

I have tried launching the application via an entry in rc.local and as a service using systemd. Again while the application launches and runs with either method, it does not pick up config changes.

System Details:

  • .Net Core 2.1
  • OS Build: Raspbian 4.14.52-v7+ #1123

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Register Hosted Services

        services.AddOptions();
        services.AddSingleton<IHostedService, MonitorService>();
        services.Configure<AppSettings (Configuration.GetSection("AppSettings"));
        services.AddMvc();

    }

Service Constructor

public MonitorService(IOptionsMonitor appSettings) {

    this.AppSettings = appSettings;
}

rc.local

 #!/bin/sh -e

 sudo  /home/pi/App/MonitorApp &

 exit 0

MonitorApp.service

 [Unit]
 Description=My Sample Service
 After=multi-user.target

 [Service]
 Type=idle
 ExecStart=/home/pi/App/MonitorApp
 User=pi

 [Install]
 WantedBy=multi-user.target
  • Using rc.local is not recommended because of compatibility issues. MonitorApp.service is the right way and looks good except Type=idle. But it seems it does not fit the environment. Where is the config file exactly (path) located? – Ingo Oct 25 '18 at 19:38
  • Yeah that was my attempt to see if anything was different from running a service but the behaviour is the same over both approaches. – Fuzzytrooper Oct 30 '18 at 9:12
  • I can't help you if you don't answer my questions. Again, where is the config file exactly (path) located? – Ingo Oct 30 '18 at 9:15
  • It's in the same path as the application /home/pi/App/MonitorApp – Fuzzytrooper Oct 30 '18 at 12:55
0

I assume you started the application MonitorApp manually as user pi and you are in /home/pi/App as working directory and it runs still in the background after starting like a service and the application does not need any network connections and it does not do any graphical output and it does not write to any other file, or does not do any other unknown input or output. If an application is started by systemd there is no default environment set. You have to define everything the application needs to run. This could also be the working directory. So your unit file could look like this:

[Unit]
Description=My Sample Service
After=multi-user.target

[Service]
ExecStart=/home/pi/App/MonitorApp
User=pi
WorkingDirectory=/home/pi/App

[Install]
WantedBy=multi-user.target

Then enable the service and reboot:

rpi ~$ sudo systemctl enable MonitorApp.service
rpi ~$ sudo systemctl reboot

Check with:

rpi ~$ systemctl status MonitorApp.service
rpi ~$ journalctl --boot --unit=MonitorApp.service
  • Thanks - the issue was that I had not set WorkingDirectory in the service file. I'm surprised though that the configuration is correctly loaded the first time but not subsequently reloaded. I guess I have to do some more research on services but thanks for your help. – Fuzzytrooper Oct 30 '18 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.