1

我需要让我的 Java 程序每天在 linux 机器上运行一次。所以我用一行创建了一个简单的文件:

java -jar /opt/location/my_jar.jar

并将其放入 etc/cron.daily 中,假设它每天运行一次。但它根本不运行。我尝试使用 .sh 扩展名,或者只是没有扩展名的文件名。尽管如此,还是没有运气。

我用谷歌搜索了它,我得到了很多相互矛盾的信息。有人可以帮忙吗?

编辑:

根据 Satish 和 Mithrandir 给出的答案,我正在总结它现在的位置。

1.我使用 vi 创建了 run_conversions 脚本,以解决 Windows 上的行尾字符问题。现在脚本是

#!/bin/sh
/usr/bin/java -jar /opt/location/my_jar.jar

2.我把它放在/etc/cron.hourly。

3.检查 /var/log/cron 的日志我看到它每小时开始 run_conversions 并完成 run_conversions。到目前为止,一切都很好。

4.但我的 jar 文件似乎没有运行。我知道这一点,因为当它正常运行时,它应该更新数据库——而数据库没有更新。

5.这是奇怪的事情:当我手动运行 cron.hourly 时,但调用

run-parts /etc/cron.hourly

jar 文件被正确命中,并且数据库正在更新。

总结一下:通过运行部件运行它时,它可以工作。当让它每小时运行一次时,它不会。

有任何想法吗?

编辑2:

根据 Satish、Mithrandir 和 vahid 的建议,我将 run_conversions_loc 更改为如下所示:

#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bexport SHELL=/bin/bash
/usr/bin/java -jar /opt/tf/conversions/aff_networks2.jar > /opt/tf/conversions/runconversions.txt

我从 cron.hourly 中删除了脚本,并将这一行添加到 crontab:

*/10 * * * * /opt/tf/conversions/run_conversions_loc

该脚本现在每 10 分钟运行一次,并在 cron 日志中注册,如下所示:

Feb 24 09:30:01 backsome CROND[7933]: (root) CMD (/opt/tf/conversions/run_conversions_loc)

到目前为止,它看起来不错。但是数据库——它应该更新——没有更新。

ftr.properties深入研究一下,jar 文件 aff_networks2.jar 正在本地目录中寻找一个文件——与它所在的目录相同。该文件存在于该目录中。但它没有正确读取。我知道这是因为在输出文件 runco​​nversions.txt 中,应该从属性文件中读取的值是 null。

完成图片需要两件事:

  1. 转换目录中的所有内容都有 777 权限。我知道不建议提供这样的扩展权限,但我想确保(至少尝试)这不是问题。

  2. 当我通过调用它从 shell 运行脚本时./run_conversions_loc,会找到属性文件并更新数据库。我以 root 身份登录到 shell,我还以 root 身份创建了所有相关文件,并以 root 身份安装了用于在 crontab 中调用脚本的行。

有什么想法为什么 cron 不读取属性文件?

4

3 回答 3

2

示例解决方案:

在命令行上手动运行命令:

[root@04 cron.daily]# /usr/bin/java -jar /root/HelloWorld.jar
Hello World #1

让我们在 / 中创建一个脚本etc/cron.daily/test.sh并赋予执行权限:

#!/bin/bash
/usr/bin/java -jar /root/HelloWorld.jar

注意:运行dos2unix以防您遇到 dos 字符问题或错误/bin/bash^M: bad interpreter: No such file or directory

[root@04 cron.daily]# unix2dos test.sh
unix2dos: converting file test.sh to DOS format ...

测试一下,瞧!!

[root@04 cron.daily]# run-parts /etc/cron.daily
/etc/cron.daily/ldiscan:

kcore: Value too large for defined data type
/etc/cron.daily/test.sh:

Hello World #1
于 2013-02-22T17:09:18.660 回答
2

its probably your environment variables

does it work as the current user logged in when executing the script ?

if so

run:

env|egrep "(^PATH=|^SHELL=)"|awk '{print "export "$1}'

then take the output and put it on the top of your script and try another cron in 2 minutes from now to see if it worked

Updated answer in response to Eddy's Comment 24th Feb 2013.

I want to give you a crash course on crontab.

  1. setting up crontab can be done via global /etc/crontab or under user as crontab -e (to edit specific user's cron) or crontab -l (lists - stored in /var/spool/cron for each user)

I see you are trying to attempt a run ever 10 minutes which is fine in /etc/crontab

The reason why I suggested giving the entire class path of your current shell is because most of the time the script is trying to use a unix command that is not available as part of the crontab's PATH (which resides right at the very top of /etc/crontab file itself)

To debug path issues its usually a good idea to watch the mailbox of the user that crontab is executing the task as : so tail -100 /var/spool/mail/root and looking out for any messages related to that cron task as well as the cron logs itself as someone has suggested -

I do not think your problem is paths here though..

You are trying to run a java jar file and it may be that your jar file needs other files in that conversion folder and that when you are running it you are already in that folder....

so in your script you could run

cd /opt/tf/conversions/;
 /usr/bin/java -jar aff_networks2.jar > /opt/tf/conversions/runconversions.txt

But since this is such a small script you could get away with placing the entire thing as a cron entry and bypassing a shell script altogether something like this

*/10 *    * * *   root cd /opt/tf/conversions/; /usr/bin/java -jar aff_networks2.jar > /opt/tf/conversions/runconversions.txt

Hope this helps solve this issue

于 2013-02-21T16:58:56.120 回答
0

将您的文件更改为:

#!/bin/sh
/usr/bin/java -jar /opt/location/my_jar.jar

检查是否/usr/bin/java实际上是安装命令的位置 ( which java)。将文件的权限更改为可执行:

chmod +x /opt/location/my_jar.jar
于 2013-02-21T16:47:56.623 回答