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?
CodePudding user response:
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.
CodePudding user response:
Yes, it is possible for a C# application to access and edit cron jobs on an Ubuntu system. However, in order to do this, you will need to use the Linux crontab command to manipulate the cron jobs.
Here are the steps you can follow to access and edit cron jobs from a C# application:
Install the cron package on your Ubuntu system. This will provide the crontab command that you will use to manipulate the cron jobs.
In your C# application, use the System.Diagnostics.Process class to run the crontab command and pass it the appropriate arguments.
For example, you can use the following code to list the current cron jobs on the system:
using System.Diagnostics;
// ...
// run the `crontab -l` command to list the current cron jobs
var crontabListProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "crontab",
Arguments = "-l",
UseShellExecute = false,
RedirectStandardOutput = true,
}
};
crontabListProcess.Start();
// read the output of the `crontab -l` command
string crontabOutput = crontabListProcess.StandardOutput.ReadToEnd();
In this code, the crontab -l command is executed and its output is captured in the crontabOutput variable.
- You can use the crontab command to add or edit cron jobs by using the -e flag and passing it the new cron job definitions.
For example, you can use the following code to add a new cron job that runs the my-script.sh script every hour:
using System.Diagnostics;
// ...
// run the `crontab -e` command to edit the cron jobs
var crontabEditProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "crontab",
Arguments = "-e",
UseShellExecute = false,
RedirectStandardInput = true,
}
};
crontabEditProcess.Start();
// write the new cron job definition to the `crontab -e` command
string newCronJobDefinition = "0 * * * * /path/to/my-script.sh";
crontabEditProcess.StandardInput.WriteLine(newCronJobDefinition);
// save the changes to the cron jobs
crontabEditProcess.StandardInput.WriteLine("\x04");
In this code, the crontab -e command is executed and the new cron job definition is written to its standard input. The \x04 character is used to signify the end of input and trigger the crontab -e command to save the changes to the cron jobs.
Keep in mind that in order for the crontab command to be able to modify the cron jobs, the user running the C# application must have permission to edit the cron jobs on the system. Depending on your setup, you may need to run the C# application as the root user or use sudo to elevate its privileges.