Core: How to run published console app on system startup linux?

Created on 19 Nov 2017  路  5Comments  路  Source: dotnet/core

I have made C# Core console application to read xml files after certain time and insert this data to database.
I need this published application to run after system startup.

I have made service file but it's not starting console application.

[Unit]
    Description=websensor

    [Service]
    WorkingDirectory=/var/aspnetcore/websensor/publish
    ExecStart=/opt/dotnet/dotnet /var/aspnetcore/websensor/WebSensor.dll
    Restart=always
    RestartSec=10
    SyslogIdentifier=websensor
    User=marek
    Environment=ASPNETCORE_ENVIRONMENT=Development

    [Install]
    WantedBy=multi-user.target

Comand /opt/dotnet/dotnet /var/aspnetcore/websensor/WebSensor.dll is working.
sudo systemctl status websensor.service gives (code=exited, status=0/SUCCESS)

Most helpful comment

How do you expect Console.ReadLine() to work in a service? I would expect that services don't receive any input, so the Console.ReadLine() will return immediately, shutting down your application prematurely.

Could you change your code so that it actually waits for the timer handler to finish?

All 5 comments

From your last line it appears that the systemd does work, as the exit status is 0 or Successful.

sudo systemctl status websensor.service gives (code=exited, status=0/SUCCESS)

Is your WebSensor.dll setup to start and then monitor the directory for files, or when it is run does it do a one time scan and then import the xml to your DB?

If it is the first, you will want to write a wrapper to run it every so often or monitor the directory for updates and run as needed.
If the second, Then there doesn't seem to be enough info to identify the cause. Do you have anything in the application code that would trigger a successful exit?

As a novice programer and as a system admin, this is what I understand from what is provided.
Hope it helps, I'll watch for an update.

Program will check url http://ipadress/values.xml file after certain time:

static void Main(string[] args)
{
    Timer sensorTimer = new Timer(2000); // 90000ms = 15 minutes
    sensorTimer.Elapsed += SesorTimer_Ellapsed;
    sensorTimer.Start();
    Console.ReadLine();
  }

Result success I can check from database or write something to console.

How do you expect Console.ReadLine() to work in a service? I would expect that services don't receive any input, so the Console.ReadLine() will return immediately, shutting down your application prematurely.

Could you change your code so that it actually waits for the timer handler to finish?

Thank you @svick. I change my code not to use timer and service. I made schedule task to run this application after 15minutes with cron.
crontab -e
*/15 * * * * /opt/dotnet/dotnet /var/aspnetcore/websensor/WebSensor.dll

FWIW, you can still use timers with systemd (assuming you prefer that). For some examples, see https://wiki.archlinux.org/index.php/Systemd/Timers.

Was this page helpful?
0 / 5 - 0 ratings