简体   繁体   English

shell脚本在由cronjob执行时失败,否则工作正常

[英]shell script fails when executed by cronjob, works fine otherwise

I have an executable jar and I have written a shell script to execute it. 我有一个可执行jar,我编写了一个shell脚本来执行它。 When I run the shell script manually, it runs fine but when schedule to run it weekly using crontab, it gives the following error - 当我手动运行shell脚本时,它运行正常但是当计划每周使用crontab运行它时,它会出现以下错误 -

log_process.sh: line 16: java: command not found

Line 16 in my shell script is - 我的shell脚本中的第16行是 -

java -jar $jar_path $logDirectory $logNamePattern $processedLogDirectory $oldResultsDirectory 2>>$log_file 1>&2

Any idea why is it happening that it runs fine when I run it manually but not when it gets run by vrontab job? 任何想法为什么它在我手动运行时运行正常而在vrontab作业运行时却没有运行?

Your cron job doesn't have access to the same $PATH variable that you as a user have. 您的cron作业无权访问您作为用户所拥有的相同$ PATH变量。

The easiest way to fix this is to open up a terminal, and run this command: 解决此问题的最简单方法是打开终端,然后运行以下命令:

which java

That's going to give you the absolute path of your java executable. 这将为您提供java可执行文件的绝对路径。 For example: 例如:

/opt/Oracle/Java/bin/java

Replace your 'java' command with the whole path. 用整个路径替换'java'命令。

You might also want to specify the JAVA_HOME variable in your shell script. 您可能还想在shell脚本中指定JAVA_HOME变量。 From your terminal run: 从您的终端运行:

echo $JAVA_HOME

That'll give you another path, like '/opt/Oracle/Java'. 那会给你另一条路,比如'/ opt / Oracle / Java'。 In your script (assuming you are using bash), before you run the java command, put: 在您的脚本中(假设您正在使用bash),在运行java命令之前,请执行以下操作:

export JAVA_HOME=/opt/Oracle/Java

Replacing '/opt/Oracle/Java' with the output that the previous echo gave you. 用前一个echo给你的输出替换'/ opt / Oracle / Java'。

暂无
暂无

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

相关问题 Java中的正则表达式模式失败,但可以正常工作 - Regex pattern in java fails but works fine otherwise 升级后,@MapsId 在保存现有实体时抛出错误,但其他工作正常 - Post-upgrade, @MapsId is throwing an error when saving an existing entity, but otherwise works fine 读取资源文件时,Teamcity构建失败,但在本地运行良好 - Teamcity build fails when reading resource file but works fine locally SOAP 调用在 websphere 上部署时失败,但在 tomcat 上工作正常 - SOAP call fails when deployed on websphere but works fine on tomcat ProGuard-应用程序可在模拟器中正常运行,但在尝试构建APK时失败 - ProGuard - Application works fine in emulator, but fails when trying to build apk 通过Runtime.exec()执行时命令失败,但在Raspberry Pi(Linux)上手动执行时命令起作用 - Command fails when executed through Runtime.exec() but works when executed manually at Raspberry Pi (Linux) Shell 文件在手动运行时有效,但在 CRON 中运行时失败 - Shell file works when run manually but fails when run in CRON java - 通过Java DefaultExecutor和CommandLine执行shell脚本时如何检索可能的shell脚本退出代码? - How to retrieve possible shell script exit codes when Executed shell script by Java DefaultExecutor & CommandLine? Shell 命令从 Java 失败,但在手动运行时有效 - Shell command fails from java but works when run manually 从shell脚本创建时,java进程创建失败 - java process creation fails when creating from shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM