簡體   English   中英

在bash腳本上找不到bundle命令

[英]bundle command not found on bash script

我寫了一個腳本,可以在crontab上重新啟動時自動運行

這是我在crontab -e配置

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

這將啟動腳本並在/home/deploy創建日志

這就是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

當我使用./startup_script手動運行此腳本時,它可以正常運行,但是當我在crontab中自動運行它時,即使我已經安裝了捆綁器, bundle command not foundbundle command not found

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

您的crontab使用的環境將不同於您的常規登錄Shell。

現在,我對此可能是錯的,但是我認為在執行crontab時,它不是登錄外殼,因此它沒有在.bashrc.bash_profile添加任何內容。

此處的最佳實踐是對bundle使用可執行文件的完整路徑。

  1. stderr重定向到stdout ,應該有2>&1
  2. gem軟件包的安裝路徑是否已添加到$PATH變量中? 嘗試提供此腳本的完整路徑
  3. 我建議您輸入一個條目,以查看crontab具有哪些環境變量:
     * * * * * printenv > ~/printenv.log 

cron經常清除整個環境,包括$PATH變量。 因此,腳本在cron中的行為可能與shell中的行為有所不同。 為了避免必須鍵入命令的絕對路徑,shell引入了$PATH環境變量,每個目錄都由:分隔,並從左到右進行搜索。

選項I:您可以使用絕對路徑:

以sudoer的身份運行哪個捆綁軟件,以獲取bundle命令的完整路徑。 如果輸出為/usr/bin/bundle ,則腳本中的bundle命令將如下所示:

/usr/bin/bundle install

選項II:設置PATH變量:

以運行此腳本的用戶身份運行echo“ $PATH ”,以獲取$PATH變量,並確保該變量在cron腳本中也可用。 例如,如果輸出為/usr/local/bin:/usr/bin:/bin ,則將以下行放在shell腳本的頂部:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM