LoginSignup
5

More than 5 years have passed since last update.

さくらレンタルサーバーでs3cmdをcronで動かす時にはまったこと

Posted at

結論

最初に結論を書くと、Pythonまでのパスをちゃんと通さないとs3cmdが動かない。

現象

適当にs3cmdでputするshellを作成する。

put.sh
#!/bin/bash

/path/to/s3cmd  --config=/path/to/.s3cfg put /path/to/file.txt s3://bucket_name/file.txt

sshから実行すると問題なく動く。

% bash /path/to/put.sh

定期実行させたかったので、さくらの管理画面にあるcron設定から実行コマンドを設定。

cron実行コマンド
cd /home/username/; /usr/local/bin/bash put.sh 1> /dev/null

s3にfile.txtがputされない。

原因

ssh接続時のPATHとcron実行時のPATHの値が違うせいで、s3cmd内で使用するPythonが見つからない。

Pythonの場所

/usr/local/bin/python

ssh接続時のPATH

/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/username/bin

cron実行時のPATH

/usr/bin:/bin:/

/usr/local/binがない!

対策

実行したいshellの中でPythonのパス解決をしてあげる

put.sh
#!/bin/bash

PATH="$PATH":/usr/local/sbin:/usr/local/bin

/path/to/s3cmd  --config=/path/to/.s3cfg put /path/to/file.txt s3://bucket_name/file.txt

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5