简体   繁体   English

在Linux OpenSuse cron上,脚本运行,但仅生成空日志

[英]On Linux OpenSuse cron, scripts run, but generate only empty logs

I have OpenSuse Linux 12.3, and I have a script file that runs every half hour. 我有OpenSuse Linux 12.3,并且有一个脚本文件,每半小时运行一次。 I know it runs because it generates a log file. 我知道它可以运行,因为它会生成一个日志文件。 The log file as as follows: myscript.sh 日志文件如下:myscript.sh

I should say that this script runs fine from the command line. 我应该说这个脚本可以从命令行正常运行。 It also runs fine in Task Scheduler if I tell it to "run now", it does run, and does generate a log file, and the log file has the appropriate log data in it. 如果我告诉它“立即运行”,它也可以在Task Scheduler中正常运行,它确实在运行,并且确实生成了一个日志文件,并且该日志文件中包含适当的日志数据。

I did change the script to chmod 777, so everything has access to it. 我确实将脚本更改为chmod 777,因此所有人都可以访问它。 I know the cron service is running on the system. 我知道cron服务正在系统上运行。 I can see log files being generated, but there is nothing in the log. 我可以看到正在生成日志文件,但是日志中没有任何内容。 They are 0 byte size files. 它们是0字节大小的文件。 This is when the Task Scheduler goes to run something. 这是任务计划程序要运行某些程序的时间。

So, again, here is the script: myscript.sh: 因此,这里再次是脚本:myscript.sh:

datestr=$(date +%Y%m%d_%H%M%S)
cd /home/tholmes/git/Tom_Utils/Tom_Utils
mvn exec:java -Dexec.mainClass="com.tom.test.utils.fix.MyJavaApp" -Dexec.args="12" > /home/tholmes/logs/my_java_app/12_$datestr.log

So, I do know that all the commands in this script work perfectly? 因此,我知道该脚本中的所有命令都能正常工作吗? When I run the script in the Task Scheduler manually, it works fine, and a log is generated. 当我在“任务计划程序”中手动运行脚本时,它可以正常工作,并生成日志。 But when the Task Scheduler auto-runs, it doesn't run... 但是当任务计划程序自动运行时,它不会运行...

Can anyone help me what I did wrong? 谁能帮助我我做错了什么? I did not expect this to be so difficult. 我没想到这会如此困难。

Capture stderr by appending 2>&1 . 通过附加2>&1捕获stderr Also, you can break up long line with a single \\ like 另外,您可以用一个\\分隔长行

mvn exec:java -Dexec.mainClass="com.tom.test.utils.fix.MyJavaApp" \
    -Dexec.args="12" > /home/tholmes/logs/my_java_app/12_$datestr.log 2>&1

You can also write stderr to a second file, 您也可以将stderr写入第二个文件,

mvn exec:java -Dexec.mainClass="com.tom.test.utils.fix.MyJavaApp" \
    -Dexec.args="12" > /home/tholmes/logs/my_java_app/12_$datestr.log \
    2>/home/tholmes/logs/my_java_app/12_$datestr.err

I found 3 other people who asked the same question, namely: 我发现另外3个人提出相同的问题,即:

Why won't a cron job, which is executing a script, NOT execute specifically maven commands? 为什么不执行正在执行脚本的cron作业,而不执行特定的maven命令?

1) The first thing I did was download maven locally to my /opt/maven-3.2.3 directory. 1)我做的第一件事是将maven本地下载到我的/opt/maven-3.2.3目录中。 Normally, when I am developing my java code, maven is running from the built in maven within eclipse. 通常,当我开发Java代码时,Maven从Eclipse中的内置Maven运行。

2) Within my script I added the exports to for JAVA_HOME, JRE_HOME, MAVEN_HOME as follows: 2)在我的脚本中,我将导出添加到JAVA_HOME,JRE_HOME,MAVEN_HOME如下:

export JAVA_ROOT=/opt/java
export MAVEN_HOME=/opt/maven
export MAVEN_BINDIR=/opt/maven/bin
export JAVA_HOME=/opt/java
export JAVA_BINDIR=/opt/java/bin
export JRE_HOME=/opt/java/jre
export JRE_BINDIR=/opt/java/jre/bin
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH
export PATH=${MAVEN_HOME}:${MAVEN_BINDIR}:$PATH
export PATH=${JRE_HOME}:${JRE_BINDIR}:$PATH
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH

So, the path to java and maven were both setup within the script. 因此,java和maven的路径都在脚本中设置。

3) Finally, I made sure that I made sure that mvn was called from it's location (/opt/maven/bin), and this may have been overkill since the path was established, but I wanted to make sure the maven command was going to be executed from the correct location. 3)最后,我确保从其位置(/ opt / maven / bin)调用了mvn,并且自从建立路径以来,这可能已经过头了,但是我想确保maven命令正在执行从正确的位置执行。

/opt/maven/bin/mvn exec:java -Dexec.mainClass="com.tom.test.utils.fix.MyJavaApp" -Dexec.args="12"

This solved my problem, and I hope this helps someone else. 这解决了我的问题,希望对其他人有所帮助。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM