How to create a Linux Cron Job with .NET Core

.NET Core Linux Cron
The ecosystem of .NET technologies allows you to build a wide range of applications: web apps, desktop apps, services and much more. One often neglected technology is the humble Console Application.

If you are working in an enterprise setting, utilising Azure to host and run services as a Azure Functions and WebJobs makes life easier.

But what about hosting on Linux?

With .NET Core, you can! That's why, in this article, I will describe how to create a passive Console Application which runs as a cron job on a Linux server such as a Ubuntu.

You will need...



  • dotnet SDK installed

  • Command prompt (cmd.exe)

  • Notepad (or another basic text editor)

  • Ubuntu server via SSH or the terminal

  • sftp and cron installed on your Ubuntu server


Building a passive console application



The easiest way to create a simple console application does not even require you to open Visual Studio.

Instead, open Command Prompt (Windows Key + R, type cmd, hit Enter).

Use the cd command to change directory and navigate to where you want to create your test application. I'm using C:\Users\AntonyAllen\source\repos>

Once in your directory, use the mkdir command to make a new folder for you:

mkdir MyLinuxApp
cd MyLinuxApp


Now to create the console application in this folder.

dotnet new console


Now, let's edit our application.

notepad Program.cs


Change the contents of the static void Main function to:

Console.WriteLine("This is a test!");

Now save and close Notepad.

Verify that your changes were saved by typing the following in Command Prompt:

more Program.cs


What is a passive application?



By passive, I mean to say that there should be no interaction with the user in the console application. So, no Console.ReadKey() or .ReadLine(), or any other function which requires user input. When running as a background process in Linux, this will cause an infinite loop to occur.

Build and Publish



Now we need to publish our application.

For the sake of simplicity, we can publish a self-contained application that we can deploy to our Ubuntu server. However, it is of course possible to install the dotnet runtime directly on Ubuntu, so we do not need to publish the Core DLL files.

dotnet publish -c release -r ubuntu.18.04-x64


This will publish for the 64-bit edition of Ubuntu 18.04, so you may need to change the runtime accordingly.

Now, navigate through the directory structure to see what was published:

cd bin
cd release
cd net5.0
cd ubuntu.18.04-x64
cd publish
dir


As you can see, there are a lot of .dll files in that folder, and some *.so files, amongst others.

Your executable is called MyLinuxApp (without an extension).

Transfer files to your Ubuntu server



For this next stage, I won't go into detail, but essentially, you need to transfer the contents of the "publish" folder we just created to your Ubuntu server.

Use sftp from Command Prompt as follows:

sftp myusername@myhostname.com


Once you're in, you can use the recursive put command to place the files into your desired folder on the server:

put -r "C:\Users\AntonyAllen\source\repos\MyLinuxApp\bin\release\net5.0\ubuntu.18.04-x64\publish" "MyLinuxApp"


Obviously, you need to provide the correct directory from your local machine.

Once the files are uploaded, get out of sftp by typing:

exit


Run the app inside Ubuntu



Use SSH to get into your server from Command Prompt

ssh myusername@myhostname.com


Locate the folder that you just uploaded.

cd MyLinuxApp/
ls -l


If you've done everything correctly, you should see the published files on your Ubuntu server.

Set the executable permission



In order to run our app, we need to add the right permission:

chmod +x MyLinuxApp


Run the app in SSH



Let's try running our app:

./MyLinuxApp


If all is well, you will see "This is a test!"

Setup the Cron Job



Now to configure our passive console application to run as a "service" in Linux Ubuntu.

More specifically, we will use the cron system to schedule the app every 1 minute.

To access the cron task definition list, type:

crontab -e


Now, you can use GNU nano or another text editor to add the following line to the end of the file:

* * * * * cd ~/MyLinuxApp && ./MyLinuxApp > ~/test.log


Now hit Ctrl+X, Y, and Enter to save and close the file.

Wait a minute for the cron job to run, and then type:

less ~/test.log


If the file exists, and you read the contents "This is a test!" then congratulations, you have just created your first Ubuntu cron job running a .NET console application!

Summary



  1. Create a passive Console Application in .NET Core (3.1, 5.0, 6.0, etc.)

  2. Publish the app with dotnet publish for the target environment

  3. Transfer files to your Linux server

  4. Set the correct permissions using chmod

  5. Add the task to the crontab


Thanks for reading! Please share this article and come back soon! 😊
Hey you! I need your help!

Thanks for reading! All the content on this site is free, but I need your help to spread the word. Please support me by:

  1. Sharing my page on Facebook
  2. Tweeting my page on Twitter
  3. Posting my page on LinkedIn
  4. Bookmarking this site and returning in the near future
Thank you!