How to make cron work on Centos 7 dotnet core

(Uncategorized)

Since the initial release of dotnet core for cross platform I finally started to consider c# as viable language for other environments than linux.  My first application is a very simple one but does contain such details as using http client.  In development all went smoothly.  Even running the app from production server by hand worked.  However when attempting to run the app in a cron job I was met with a cryptic message:

The type initializer for ‘System.Net.Http.CurlHandler’ threw an exception.

After some digging around I realized that the environment variables Centos cron runs on, are woefully inadequate to run dotnet application.  Meaning the dotnet installer on Centos fails to fill out the necessary information.

Adding the following lines to /etc/environment file fixed my particular problems:

export ASPNETCORE_ENVIRONMENT=Production
export PATH="/opt/rh/rh-dotnet20/root/usr/bin:/opt/rh/rh-dotnet20/root/usr/sbin${PATH:+:${PATH}}"
export CPATH="/opt/rh/rh-dotnet20/root/usr/include${CPATH:+:${CPATH}}"
export LD_LIBRARY_PATH="/opt/rh/rh-dotnet20/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
export MANPATH="/opt/rh/rh-dotnet20/root/usr/share/man:${MANPATH:-}"
export PKG_CONFIG_PATH="/opt/rh/rh-dotnet20/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}"
export PYTHONPATH=/opt/rh/rh-dotnet20/root${PYTHONPATH:+:${PYTHONPATH}}
export XDG_DATA_DIRS=/opt/rh/rh-dotnet20/root/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
export DOTNET_CLI_TELEMETRY_OPTOUT=false
LD_LIBRARY_PATH=/opt/rh/rh-dotnet20/root/usr/lib64
CPATH=/opt/rh/rh-dotnet20/root/usr/include
PATH=/opt/rh/rh-dotnet20/root/usr/bin:/opt/rh/rh-dotnet20/root/usr/sbin:/sbin:/bin:/usr/sbin:/usr/bin
PKG_CONFIG_PATH=/opt/rh/rh-dotnet20/root/usr/lib64/pkgconfig