通过Red Hat上的cron运行dotnet失败

问题描述 投票:0回答:1

我们有一个dotnet核心脚本,用于索引某些文件。我们利用RedHat Software Collection,使诸如dotnet之类的项目可以绑定到我们的RHEL设置中。

要运行脚本,我们执行以下操作: source scl_source enable rh-dotnet30 /opt/rh/rh-dotnet30/root/usr/bin/dotnet /d/h/fileprocessor.dll 1

我们想在cron中运行它,但是我们无法使其正常工作。我们尝试了以下方法:

  1. 向bash配置文件添加'source'命令,但这对我们来说似乎不可靠,并且不会在cron事件上运行。
  2. 直接在cron中运行
  3. 在cron中作为shell脚本运行

我们很茫然,似乎我们永远无法使这两个命令协同工作。如果我们不包括source命令,即使在我们的配置文件中,该命令也不会运行,并显示错误消息“无法找到任何已安装的.NET Core SDK。您是要运行.NET Core SDK命令吗?从以下位置安装.NET Core SDK:https://aka.ms/dotnet-download

.net-core cron rhel rhel7 software-collections
1个回答
0
投票

以下对我有用。我正在使用rh-dotnet31(.NET Core 3.1),因为rh-dotnet30(.NET Core 3.0)已不受支持:

  1. 安装软件包:

    $ sudo yum install rh-dotnet31 -y
    
  2. 从已知目录开始

    $ cd ~
    
  3. 为.NET Core源代码创建目录以供使用

    $ mkdir hello
    $ cd hello
    
  4. 创建一个简单的测试应用程序

    $ scl enable rh-dotnet31 bash
    $ dotnet new console
    $ dotnet publish
    $ exit      # this exits from the subshell started from scl enable command above
    
  5. 将构建复制到一个单独的目录中,我们可以在其中运行它

    $ cp -a bin/Debug/netcoreapp3.1/publish ../hello-bin
    
  6. 创建cron将调用的脚本

    $ cd ~
    

    并将其放入./test.sh文件:

    #!/ bin / bash回显“ test.sh现在正在运行...。”源scl_source启用rh-dotnet31dotnet $ HOME / hello-bin / hello.dll 1

    您甚至可以将最后两行(source...dotnet ...)合并为scl enable rh-dotnet31 -- dotnet $HOME/hello-bin/hello.dll 1

    然后将其设为可执行文件:

    $ chmod +x ./test.sh
    
  7. 设置crontab文件

    $ crontab -e
    

    然后在此文件下面添加以下行。此脚本每分钟运行一次脚本。

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

在我的机器上,cron正在运行,因此几分钟后,我现在在日志文件中看到cron作业的输出:

$ 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!
© www.soinside.com 2019 - 2024. All rights reserved.