我们有一个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中运行它,但是我们无法使其正常工作。我们尝试了以下方法:
我们很茫然,似乎我们永远无法使这两个命令协同工作。如果我们不包括source命令,即使在我们的配置文件中,该命令也不会运行,并显示错误消息“无法找到任何已安装的.NET Core SDK。您是要运行.NET Core SDK命令吗?从以下位置安装.NET Core SDK:https://aka.ms/dotnet-download“
以下对我有用。我正在使用rh-dotnet31
(.NET Core 3.1),因为rh-dotnet30
(.NET Core 3.0)已不受支持:
安装软件包:
$ sudo yum install rh-dotnet31 -y
从已知目录开始
$ cd ~
为.NET Core源代码创建目录以供使用
$ mkdir hello
$ cd hello
创建一个简单的测试应用程序
$ scl enable rh-dotnet31 bash
$ dotnet new console
$ dotnet publish
$ exit # this exits from the subshell started from scl enable command above
将构建复制到一个单独的目录中,我们可以在其中运行它
$ cp -a bin/Debug/netcoreapp3.1/publish ../hello-bin
创建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
设置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!