LoginSignup
7
9

More than 5 years have passed since last update.

cronをシンボリックリンクにしている場合にcapistranoで権限変更をしたい

Posted at

前提

  • capでデプロイしている
  • 本番サーバーでsudoが使えるようになっている
  • cronの設定ファイルをプロダクトのgitレポジトリ内に含めている

問題

  • cronをシンボリックリンクで/etc/cron.d/以下に設置しても権限の問題で無視される
    • 以下の設定を追加
      • ownerはroot:root
      • permissioは644
  • gitで予めchown root:rootしておけない(よね?)

解決方法

  • capでデプロイする度に権限を変更する

実装

sudoを使うので以下を設定してなかったら追加

config/deploy.rb
+set :pty, true

taskを追加したい場合は、Capfileの中身を見てそこに設置する

lib/capistrano/tasks/symlink_cron.rake
namespace :symlink_cron do
  desc "change owner to root"
  task :change_permission do
    on roles(:app) do
      execute :sudo, "chmod 644 #{release_path}/misc/cron/sample"
      execute :sudo, "chown root:root #{release_path}/misc/cron/sample"
    end
  end
  after 'deploy:published', 'symlink_cron:change_permission'
end

ちなみに実際に設定するcronファイルの例を以下に示す

misc/cron/sample
APP_DIR=/home/your_username/your_project_name/current
LOG_DIR=/var/log/your_project_name/cron

# 毎週火曜10:00に実行
00 10 * * 2 root su your_username -l -c "cd ${APP_DIR} && bundle exec rake sample:run RAILS_ENV=production" >> ${LOG_DIR}/sample.log 2>&1

※miscってのは自分のチームの決めでつくったディレクトリ

あとは、capでもChefでもansibleでもいいですが、以下のシンボリックリンクを貼るようにしておけば、cronが実行される状態になります。

ln -s /home/your_username/your_project_name/current/misc/cron/sample /etc/cron.d/sample

参考

7
9
0

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