Running dotnet through cron on RedHat fails

627 Views Asked by At

We have a dotnet core script we use to index some files. We leverage RedHat Software Collection so items like dotnet can tie into our RHEL setup.

To run the script, we do the following: source scl_source enable rh-dotnet30 /opt/rh/rh-dotnet30/root/usr/bin/dotnet /d/h/fileprocessor.dll 1

We want to run this in cron, but we can not get it to work. We have tried the following:

  1. Adding the 'source' command to the bash profile, but this doesn't seem to be reliable for us, and not run on the cron event.
  2. Running this directly in cron
  3. Running this as a shell script in cron

We are at a loss, it seems we can never get the two commands to work together. If we don't include the source command, even if in our profile, it will not run and gives us the error " It was not possible to find any installed .NET Core SDKs Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download"

2

There are 2 best solutions below

0
jmlumpkin On BEST ANSWER

The issue we ran into for this was that the runtime had only been installed, and not the SDK. Once the SDK was installed, which included many other dependencies, it just worked.

0
omajid On

The following works for me. I am using rh-dotnet31 (.NET Core 3.1) though, since rh-dotnet30 (.NET Core 3.0) is out of support:

  1. Install packages:

    $ sudo yum install rh-dotnet31 -y
    
  2. Start at a known directory

    $ cd ~
    
  3. Create a directory for .NET Core source code to use

    $ mkdir hello
    $ cd hello
    
  4. Create a simple application for testing

    $ scl enable rh-dotnet31 bash
    $ dotnet new console
    $ dotnet publish
    $ exit      # this exits from the subshell started from scl enable command above
    
  5. Copy the build over to a separate directory where we can run it

    $ cp -a bin/Debug/netcoreapp3.1/publish ../hello-bin
    
  6. Create the script that cron will invoke

    $ cd ~
    

    And put this in a ./test.sh file:

    #!/bin/bash
    echo "test.sh running now...."
    source scl_source enable rh-dotnet31
    dotnet $HOME/hello-bin/hello.dll 1
    

    You could probably even combine the last two lines (source... and dotnet ...) into scl enable rh-dotnet31 -- dotnet $HOME/hello-bin/hello.dll 1

    Then make it executable:

    $ chmod +x ./test.sh
    
  7. Set up the crontab file

    $ crontab -e
    

    And then add the line below in this file. This one runs the script every minute.

    * * * * *       $HOME/test.sh >> $HOME/test.cron.log 2>&1
    

On my machine, cron is running, so I now see the output of the cron job in the log file after a few minutes:

$ tail -f test.cron.log                     
test.sh running now....
Hello World!
test.sh running now....
Hello World!
test.sh running now....
Hello World!
test.sh running now....
Hello World!
test.sh running now....
Hello World!