【问题标题】:Java Cron and epic failsJava Cron 和史诗失败
【发布时间】:2016-07-20 12:27:05
【问题描述】:

我在 14.04.1-Ubuntu 上运行它

Linux matlabrun 3.19.0-56-generic #62~14.04.1-Ubuntu SMP Fri Mar 11 11:03:15 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

使用java版本:

java 版本 "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64 位服务器 VM(构建 24.80-b11,混合模式)

所以我的 cron testrun 和 delme.jar 中有两个元素。

PATH=/usr/bin/java:/usr/bin/javac:/usr/lib/jvm/java-7-oracle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Runtime/v85/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v85/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v85/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v90/$
    XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Runtime/v85/X11/app-defaults:/usr/local/MATLAB/MATLAB_Runtime/v90/X11/app-defaults
    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').#
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h  dom mon dow   command
    5,10,15,20,30,35,40,43,45,50,52,55,58 * * * 0,1,2,3,4,5,6 /home/meh/model/TestModelABC/testrun > /home/meh/model/TestModelABC/testrun.log
    5,10,13,15,17,18,20,25,30,33,35,36,40,43,45,50,52,55,58 * * * 0,1,2,3,4,5,6 java -jar /home/meh/model/TestModelABC/delme.jar > /home/meh/model/TestModelABC/del.log

testrun 是一个 bash 脚本,这样调用 java 代码:

#!/bin/sh
java -jar delme.jar

这不起作用。当我从 crontab 中直接通过 java -jar delme.jar 调用文件时,它也不起作用。

我知道 cron 正在运行它只是没有执行 java。

为此,如您所见,我将路径放入 crontab 中,并尝试将路径放入 bash 脚本中,如下所示:

PATH=/usr/bin/java:/usr/bin/javac:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Runtime/v85/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v85/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v85/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v90/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v90/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v90/sys/os/glnxa64:
XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Runtime/v85/X11/app-defaults:/usr/local/MATLAB/MATLAB_Runtime/v90/X11/app-defaults
echo "Forecast Test"

我现在真的被困住了,因为我没有东西可以尝试。有任何想法吗?我的直觉告诉我这是一个 PATH 问题,但我不知道还有什么要尝试的。

【问题讨论】:

  • 作业向 stderr 写入了什么?
  • testrun 应该在哪里找到delme.jar?它应该怎么知道它住在哪里?

标签: java bash cron classpath crontab


【解决方案1】:

当我通过 java -jar 直接调用文件时它也不起作用 delme.jar

听起来你没有可执行的 jar 文件。

确保您在 jar 清单中设置了 Main-Class: classname,或者直接运行该类,在类路径中指定您的 jar。

例如java -cp delme.jar path.to.package.Main

另请参阅:How to make an executable jar file?

【讨论】:

  • 我整天都在调试cron。每个问题都是钉子,嗯。这大概就是问题所在。在 Eclipse 中,你应该使用 Export 函数来构建一个可运行的 jar。
  • @ifly6 我们都去过了,还是等OP确认吧!
  • 我很确定 jar 是可执行的,因为当我通过命令行运行 java - jar delme.jar 时它运行良好。如果我从命令行运行 ./testrun ,它也运行良好。它唯一不起作用的是从 crontab 运行时。 Cron 调用 bash 脚本正常,但是当它通过 cron 调用时,它不会运行 jar。我在 testrun.log 文件中没有得到任何东西。
  • 当我通过 java -jar 直接调用文件时它也不起作用 delme.jar 仅在 CRON 中调用时。当通过命令行这样调用时,它运行得非常好。
【解决方案2】:

事实证明,这是一个路径问题,CRON 从 /home/user 调用,因此在 -cp 中使用的任何相对路径都不再有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多