AllenC
AllenC

Reputation: 2754

bundle command not found on bash script

I'm wrote a script to automatically run when reboot on crontab

this is my configuration in crontab -e

@reboot /home/deploy/startup_script >> /home/deploy/startup_script.log 2>$1

This start the script and create logs in /home/deploy

Then this is the startup_script

#!/bin/bash

echo "Changing directory"
cd /home/deploy/source/myapp
echo $PWD

echo "Pulling Dev Branch..."
git pull origin dev_branch

echo "Running Bundle Install"
sudo gem install bundler
bundle install

echo "Deploying to Staging..."
bundle exec cap staging deploy

when I run this script manually using ./startup_script it runs properly but when I run it automatically in crontab it shoes bundle command not found even I install the bundler already.

Here's the logs from startup_script.log

Changing directory
/home/deploy/source/myapp
Pulling Dev Branch...
From ssh://1.xx.xx.xx.io:20194/xx/myapp
 * branch            dev_branch -> FETCH_HEAD
Already up-to-date.
Running Bundle Install
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Done installing documentation for bundler after 5 seconds
1 gem installed
/home/deploy/startup_script: line 12: bundle: command not found
Deploying to Staging...
/home/deploy/startup_script: line 15: bundle: command not found

Upvotes: 2

Views: 4241

Answers (3)

1218985
1218985

Reputation: 8012

The cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell. To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right.

Option I: You can use absolute path:

Run which bundle as sudoer to get the full path for the bundle command. If the output is /usr/bin/bundle, your bundle command in the script would look like:

/usr/bin/bundle install

Option II: Set the PATH variable:

Run echo "$PATH" as user who runs this script to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the below line in the top of your shell script:

export PATH="/usr/local/bin:/usr/bin:/bin"

Upvotes: 2

Gander
Gander

Reputation: 1991

  1. Redirecting stderr to stdout, there should be 2>&1
  2. Is the path where the gem packages are installed is added to the $PATH variable? Try to provide the full path to this script
  3. I suggest you make an entry to see what environment variables you have for crontab:
    * * * * * printenv > ~/printenv.log
    

Upvotes: 0

hwrd
hwrd

Reputation: 2180

The environment that your crontab uses is going to be different than your regular login shell.

Now, I might be wrong about this, but I think when the crontab executes, it's not a login shell, so it doesn't have anything you've added to your path in your .bashrc or .bash_profile.

The best practice here would be to use the full path of the executable for bundle.

Upvotes: 0

Related Questions