运行jar文件的crontab出错

时间:2023-01-28 09:15:18

I'm running Ubuntu 10.10 with open jdk and crontab. I made a neat little Java application that does some cool stuff and exported it as a runnable jar file. I can run it from the command line just fine, but I needed something that would run the file once every day. So I wrote in a cron job to run a shell script that would actually invoke the jar file. The shell script works fine when run on its own and it looks something like this:

我正在使用open jdk和crontab运行Ubuntu 10.10。我做了一个整洁的小Java应用程序,做了一些很酷的东西,并将其导出为可运行的jar文件。我可以从命令行运行它就好了,但是我需要每天运行一次文件的东西。所以我在一个cron作业中写道,运行一个实际调用jar文件的shell脚本。 shell脚本在单独运行时工作正常,它看起来像这样:

#!/bin/sh
/usr/bin/java -jar /root/proj/CoolStuff.jar

Works perfectly. So I added this to the crontab:

完美的工作。所以我把它添加到crontab:

23 14 * * * /root/proj/runScript.sh > /root/proj/log.txt 2>&1

This does not run perfectly. In fact, it doesn't run. log.txt will come out saying "Error". Pretty nondescript, right? I've checked my environment variables, nothing fancy there. Is there anything else I might be missing? Any ideas as to where to go from here? Is there any way I can run this script everyday with ease?

这不完美。事实上,它没有运行。 log.txt会出现“错误”。非常不伦不类,对吧?我检查了我的环境变量,没有什么花哨的。还有什么我可能会失踪的吗?关于从哪里去的任何想法?我有没有办法轻松地每天运行这个脚本?

2 个解决方案

#1


0  

Check the execution permission on that file.

检查该文件的执行权限。

The cron is running with different permissions, not the one you got when logged in.

cron运行时具有不同的权限,而不是登录时获得的权限。

Also you try to access /root. Try to relocate your script to another "non-root" directory.

您也尝试访问/ root。尝试将脚本重定位到另一个“非root”目录。

#2


0  

See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.

通过注释掉当前的/ usr / bin / java ..并在一行中插入set来查看crontab的环境。

Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.

现在从命令行设置> tmpEnvVarList.txt并与log.txt中的内容进行比较。

It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.

几乎可以肯定你的.profile或.bash_rc(或其他)正在设置crontab无法使用的env变量。你需要添加。 .profile等你的脚本。

cbO's ideas are good too.

cbO的想法也很好。

I hope this helps.

我希望这有帮助。

#1


0  

Check the execution permission on that file.

检查该文件的执行权限。

The cron is running with different permissions, not the one you got when logged in.

cron运行时具有不同的权限,而不是登录时获得的权限。

Also you try to access /root. Try to relocate your script to another "non-root" directory.

您也尝试访问/ root。尝试将脚本重定位到另一个“非root”目录。

#2


0  

See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.

通过注释掉当前的/ usr / bin / java ..并在一行中插入set来查看crontab的环境。

Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.

现在从命令行设置> tmpEnvVarList.txt并与log.txt中的内容进行比较。

It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.

几乎可以肯定你的.profile或.bash_rc(或其他)正在设置crontab无法使用的env变量。你需要添加。 .profile等你的脚本。

cbO's ideas are good too.

cbO的想法也很好。

I hope this helps.

我希望这有帮助。