mvn clean在终端中工作正常,但不是来自cron和bash文件

时间:2019-06-12 05:09:11

标签: bash macos maven cron

.then()只能在终端上正常运行。即使我通过双击从bash文件(.sh文件)执行同样的操作,它也能正常工作。

但是当我使用crontab触发相同操作时,出现了错误mvn clean

bash(.sh)文件具有此代码

mvn:command not found

crontab -l的输出

#!/bin/bash 
cd /Users/testautomation/Documents/Automation/TFS/Mem_Mobile 
mvn clean

错误

0 14 * * * /Users/testautomation/Documents/Automation/Schedule/Execute.sh

我已经使用自制软件安装了Maven。

From testautomation@Tests-iMac.xxx.local Wed Jun 12 14:44:01 2019 Return-Path: <testautomation@Tests-iMac.xxx.local> X-Original-To: testautomation Delivered-To: testautomation@Tests-iMac.xxx.local Received: by Tests-iMac.xxx.local (Postfix, from userid 501) id 0BE233001CB411; Wed, 12 Jun 2019 14:44:00 +1000 (AEST) From: testautomation@Tests-iMac.xxx.local (Cron Daemon) To: testautomation@Tests-iMac.xxx.local Subject: Cron <testautomation@Tests-iMac> /Users/testautomation/Documents/Automation/Schedule/Execute.sh X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=testautomation> X-Cron-Env: <USER=testautomation> X-Cron-Env: <HOME=/Users/testautomation> Message-Id: <20190612044401.0BE233001CB411@Tests-iMac.xxx.local> Date: Wed, 12 Jun 2019 14:44:00 +1000 (AEST) /Users/testautomation/Documents/Automation/Schedule/Execute.sh: line 3: mvn: command not found 输出:

mvn -version

4 个答案:

答案 0 :(得分:1)

in here一样,您可能需要添加环境变量(MAVEN_HOME)并完成PATH(其中一个用于mvn

或者至少在script.sh中做同样的事情,这意味着不要假设您将在cron会话中继承用户会话环境的 all

答案 1 :(得分:1)

在执行脚本时使用 mvn 的完整路径。

#!/bin/bash 
cd /Users/testautomation/Documents/Automation/TFS/Mem_Mobile 
/usr/local/Cellar/maven/3.6.1/bin/mvn clean

或者下面的脚本也应该起作用

#!/bin/bash 
cd /Users/testautomation/Documents/Automation/TFS/Mem_Mobile 
/usr/local/bin/mvn clean

答案 2 :(得分:1)

Cron运行non-ineractive and non-login shell

0 14 * * * . $HOME/.profile; /Users/testautomation/Documents/Automation/Schedule/Execute.sh

如果您使用更多的shell配置文件变量扩展shell脚本,则上述命令非常适合。加载bash配置文件会更新包含PATH变量在内的env变量,以包含mvns路径(在mvn安装过程中大多已添加到该路径中)

.source的同义词。

Crontab在非常有限的Shell环境中运行。设置了一些变量,例如HOME,LOGNAME和SHELL。这就是为什么我们可以使用$ HOME调用相应的配置文件的原因。

PATH应该包含您的mvn二进制文件的位置,但仅限于cron env中的主要二进制路径;因此,您的mvn二进制文件的绝对路径如前所述。

答案 3 :(得分:0)

一个不错的方法是将 mvn 的路径声明为字符串值,并使用 "$val" 调用它:

mvn="path\to\mvn" -- 等号附近没有空格

然后调用“$mvn”