crontabコマンドでファイルの内容をcrontabに登録する

crontab -r とやってしまった時の対処法

cronの設定を変えようと crontab -e と打とうとして、間違って crontab -r と打ってしまい、cronの設定が根こそぎぶっ飛んで参りました。

俺も経験したことがある。

しかし、こんなスクリプトを書いたり、いちいちcrontab -lで確認したりする必要はない。たんに「crontab FILENAME」を実行すればそのファイルをcrontabに登録してくれる。

俺の場合、 ~/.crontab というファイルを作成し、保存と同時に登録するようにしている。

Emacsでやるには

(defun shell-command-with-error-notification (cmd error-regexp)
  (let ((buf " *shell-command-with-error-notification*"))
  (shell-command cmd buf)
  (with-current-buffer buf
    (goto-char (point-min))
    (when (re-search-forward error-regexp nil t)
      (switch-to-buffer buf)
      (message "Error!")))))
(defun save-and-install-crontab ()
  (when (string-match ".crontab" (buffer-file-name))
    (shell-command-with-error-notification (concat "crontab " (buffer-file-name)) "error")
    (message "Wrote %s (Installed)" (buffer-file-name) )))
(add-hook 'after-save-hook 'save-and-install-crontab)