Search code examples
bashmacosmavencron

mvn clean works fine in terminal but not from cron and bash file


mvn clean simply works fine from terminal. Even when I am executing the same from a bash file (.sh file) by double clicking, it working fine.

But when I trigger the same using crontab I'm getting an error mvn:command not found

bash(.sh) file have this code

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

Output of crontab -l

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

Error

From [email protected]  Wed Jun 12 14:44:01 2019
Return-Path: <[email protected]>
X-Original-To: testautomation
Delivered-To: [email protected]
Received: by Tests-iMac.xxx.local (Postfix, from userid 501)
id 0BE233001CB411; Wed, 12 Jun 2019 14:44:00 +1000 (AEST)
From: [email protected] (Cron Daemon)
To: [email protected]
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: <[email protected]>
Date: Wed, 12 Jun 2019 14:44:00 +1000 (AEST)

/Users/testautomation/Documents/Automation/Schedule/Execute.sh: line 3: mvn: command not found

I have installed maven using homebrew.

mvn -version output :

Tests-iMac:~ testautomation$ mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T06:00:29+11:00)
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 1.8.0_212, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre
Default locale: en_AU, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "Mac"

Solution

  • Use the complete path of mvn while executing the script.

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

    Or below script should also work

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