0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Linux】cron③  /etc/cron.d/ に設定ファイルを置く

Last updated at Posted at 2025-08-30

はじめに

Cronジョブの設定について、/etc/cron.d/ にファイルを作成する方法についての学習記録です。実装例としてitamaeを使って設定ファイルを作成した方法をまとめます。

crontab -e との比較

/etc/cron.d      crontab -e     
システム全体のジョブを管理 特定のユーザーのジョブを設定
管理者権限が必要 各ユーザが自分のジョブを設定
ユーザー名の指定が必要 ユーザー名の指定は不要
/etc/cron.d/[ファイル名]に保存される /var/spool/cron.crontabs/に保存される

登録方法

① ファイルを作成する

  • ファイル名に拡張子は不要
$ sudo nano /etc/cron.d/myjob

② ファイル内で以下の書式で記述する

  • crontab -eで登録する場合はユーザの指定は不要
分 時 日 月 曜日 ユーザー コマンド

③ ファイルのパーミッションを644に設定する(推奨)

  • cronデーモンは/etc/cron.d/以下のファイルを読み取る
  • 実行権限は不要
  • 644(-rw-r--r--):
    所有者:読み書き可能、他のユーザーは読み取りのみ可能)にすることで、他のユーザーに編集されるリスクを防ぐことができる
# パーミッションを確認
$ ls -l /etc/cron.d/myjob
# 644に変更
$ sudo chmod 644 /etc/cron.d/myjob

実装例

構成管理ツール「itamae」を使って、テンプレートファイル(.../templates/myapp.erb)を使って、/etc/cron.d/myappを生成する

config/itamae/cookbooks/cron.rb
template "/etc/cron.d/myapp" do
  variables(
    myapp_root: Dir.pwd,
    path: ENV['PATH'],
    rails_env: ENV['RAILS_ENV'] || 'development',
    user: ENV['USER']
  )
  user 'root'
  owner 'root'
  group 'root'
  mode '0644'
end

# SELinuxが無効な環境では下記の指定は不要
execute 'restorecon cron file' do
  user 'root'
  command 'restorecon /etc/cron.d/myapp'
  action :nothing
  subscribes :run, 'template[/etc/cron.d/myapp]', :immediately
end

template "/etc/cron.d/myapp ブロック

  • variables:テンプレート内で使う変数を定義
    • myapp_root:現在のディレクトリ(Dir.pwd
  • user/owner/group:ファイルの所有者をrootに設定
  • mode '0644':パーミッションを-rw-r--r-- に設定

execute 'restorecon cron file'ブロック

Unauthorized SELinux contextのエラーが出る場合の対応※

  • command 'restorecon /etc/cron.d/myapp':SELinuxのラベルを再設定する
  • subscribes :run, 'template[/etc/cron.d/myapp]', :immediately: テンプレートが変更されたら即座にコマンドを実行する
※【補足】Unauthorized SELinux contextのエラーの意味
Unauthorized SELinux context=system_u:system_r:system_cronjob_t:s0-s0:c0.c1023 file_context=unconfined_u:object_r:user_tmp_t:s0 (/etc/cron.d/myapp)
  • system_cronjob_t(cronのプロセス)が user_tmp_t(一時ファイル用のラベル)にアクセスしようとしており、この組み合わせはポリシー上許可されていないため拒否(Unauthorized)されている
  • user_tmp_t ではなく、正しいラベルである cron_spool_t(cronジョブの設定ファイルに割り当てられるべきラベル)に再設定する必要がある
    • SELinux(Security-Enhanced Linux)は、Linuxに組み込まれたアクセス制御機構。「ラベル」と「ポリシー」で制御を行う
      • ラベル:ファイルやプロセスに付与されるタグ。アクセス制御の基準になる
      • ポリシー:どのラベルのプロセスが、どのラベルのファイルにアクセスできるかを定義
  • ラベルを変更するコマンド①【手動で変更する】
sudo chcon -t cron_spool_t /etc/cron.d/myapp
  • ラベルを変更するコマンド②【パスに基づいて正しいラベルを再設定する】(より安全)
sudo restorecon /etc/cron.d/myapp

テンプレートの例

  • variablesで設定した変数をERBファイル内で参照することができる
  • ユーザーの設定が必要
config/itamae/cookbooks/templates/myapp.erb
PATH=<%= @path %>
RAILS_ENV=<%= @rails_env %>

33 3 * * * <%= @user %> /bin/bash -c 'cd <%= @myapp_root %> && bundle exec rake cleanup:old_data'
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?