4

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 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

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"
Aserre
  • 4,916
  • 5
  • 33
  • 56
Viki
  • 77
  • 1
  • 7

4 Answers4

1

As in here, you might need to add environment variables (MAVEN_HOME) and complete the PATH (with the one for mvn)

Or at least do the same in your script.sh, meaning don't assume that you would inherit all of your user session environment in a cron session.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

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
nagendra547
  • 5,672
  • 3
  • 29
  • 43
1

Cron runs a non-ineractive and non-login shell.

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

The above command is well suited if you expand your shell script with more shell profile variables. Loading the bash profile updates the env variables including the PATH variable to include mvns path (which was mostly added to it during mvn installation).

. is a synonym for source.

Crontab runs with a very limited shell environment. Some variables such as HOME, LOGNAME and SHELL are set. This is why we could use $HOME to call the respective profile.

PATH is supposed to contain the location of your mvn binary but is limited to main binary paths in the cron env; hence the absolute path to your mvn binary works as answered before.

Juan John Mathews
  • 728
  • 1
  • 10
  • 24
0

A nice way can be declaring the path to mvn as a string value, and calling it using "$val":

mvn="path\to\mvn" --no spaces close to the equal sign

and then calling "$mvn"

masy
  • 1