Steve
Steve

Reputation: 819

Is there anyway to access and edit Cron Jobs from a C# application?

I've got a C# asp.net core app running on an Ubuntu box. I need to run some scheduled tasks and wanted some way to manage that from the app, rather than logging into a terminal.

Is there anyway the C# app can connect to cron and return current jobs, add / edit existing ones?

Upvotes: 0

Views: 435

Answers (2)

Guru Stron
Guru Stron

Reputation: 142983

Not a direct answer but if you want a controllable scheduled tasks you can consider using hosted services to perform background tasks or libraries/frameworks like Hangfire or Quartz.NET.

Upvotes: 1

Athanasios Kataras
Athanasios Kataras

Reputation: 26430

Best bet is to expand on the following code:

    Process process = new System.Diagnostics.Process ();

    Process proc = new System.Diagnostics.Process ();
    proc.StartInfo.FileName = "/bin/bash";
    proc.StartInfo.Arguments = "-c \" " + command + " \"";
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.RedirectStandardOutput = true;
    proc.Start();

This will get you started with executing bash commands and reading the output in your code.

Alternatively, as Tristan mentioned in the comments, you can certainly read/write the cron files from your code as well.

Upvotes: 1

Related Questions