简体   繁体   English

Cron Job在Linux环境中无法正常工作

[英]Cron Job not working in Linux environment

I've been running a shell script every minute between Monday and Friday between business hours, and thought it was running fine, as I can see it when I use the "crontab -l" command. 我一直在周一至周五的工作时间之间每分钟运行一次Shell脚本,并认为它运行良好,因为使用“ crontab -l”命令时可以看到它。 The script calls a piece of Java code which checks certain conditions in a postgresql table, and if those conditions are met, it writes out to another table. 该脚本调用一段Java代码,该代码检查postgresql表中的某些条件,如果满足这些条件,它将写出到另一个表中。 The script is in the following directory on our Linux server. 该脚本位于我们的Linux服务器上的以下目录中。

/var/lib/tomcat7/webapps/sh

So lets say it is called "test.sh", should my cronjob look like this this: 因此,如果我的cronjob看起来像这样,可以说它称为“ test.sh”:

* 08-18 * * 1-5 /var/lib/tomcat7/webapps/sh/test.sh

ie, it should run every minute of every day from Monday to Friday between 8 am and 18:59 pm. 也就是说,它应该在周一至周五每天上午8点至下午18:59之间的每一分钟运行。 I just tried testing it by adding dummy data to the postgresql table so that the other table should have been written to and nothing happened. 我只是尝试通过将伪数据添加到postgresql表中来测试它,以便应该将另一个表写入并且什么也没有发生。 However when i ran ./test.sh manually from the command line it worked fine. 但是,当我从命令行手动运行./test.sh时,它工作正常。 The issue seems to be with my cron. 问题似乎出在我的cron。 If I type: 如果输入:

grep CRON /var/log/syslog

I can see that it has run every minute today. 我可以看到它今天每分钟运行一次。 The output looks like this 输出看起来像这样

Nov 25 12:38:01 webserver CRON[11868]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:39:01 webserver CRON[11888]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:40:01 webserver CRON[11953]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:41:01 webserver CRON[11953]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)
Nov 25 12:42:01 webserver CRON[11985]: (ubuntu) CMD (/var/lib/tomcat7/webapps/sh/test.sh)

etc.. However after I altered the data in the postgresql table this did not seem to have any effect. 等等。但是,在我更改了postgresql表中的数据之后,这似乎没有任何效果。 It's as if the script isn't actually running. 好像脚本实际上没有在运行。 Although it runs perfectly when I run it manually from the command line. 尽管当我从命令行手动运行时它运行完美。 Has anyone ever experienced this sort of thing with cron jobs, or have any advice as to why this might be happening? 有没有人曾经在cron工作中经历过这种事情,或者对为什么会发生这种事情有任何建议? Any assistance would be appreciated. 任何援助将不胜感激。 Thanks in advance! 提前致谢!

Your working directory is not /var/lib/tomcat7/webapps/sh but / when you launch your script with /var/lib/tomcat7/webapps/sh/test.sh 您的工作目录不是/var/lib/tomcat7/webapps/sh而是/使用/var/lib/tomcat7/webapps/sh/test.sh启动脚本时的目录

You can get the current working directory with pwd command or get the script location (see this post and then : 您可以使用pwd命令获取当前的工作目录或获取脚本位置(请参阅此文章 ,然后:

cd $SCRIPTPATH
cd ../java/JProject/sr

OR 要么

change relative path ../java/JProject/src with the absolute path 用绝对路径更改相对路径../java/JProject/src

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

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