2
1

More than 3 years have passed since last update.

MacでCronを動かす

Posted at

はじめに

定期的にスクレピングしたいものがあったので、定期実行しようと思ってcronを使用しました。

cronを動かす

terminal
# 何も設定していない人は何も記述されていない状態からスタートします
$ crontab -e
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~                                                                                                                                                          
~  

cronの記述方法

crontabコマンド入力後の画面
# (分)(時)(日)(月)(曜) (実行したいコマンド)
# 例) 毎日午前8時にPuppeteerのスクレピングファイルを実行したい
0 8 * * * /usr/local/bin/node [実行したいファイルの絶対パス]
  • * : 全ての値(トランプでいうジョーカ的役割)

注意点

▶︎ /bin/sh: ~~: command not foundエラー

whichでコマンドの絶対パスを確認して、絶対パスで記述する必要がある

terminal
# NG
# crontab設定
0 8 * * * node [実行したいファイルの絶対パス]

# OK
$ which node
/usr/local/bin/node

# crontab設定
0 8 * * * /usr/local/bin/node [実行したいファイルの絶対パス]

番外編

ログを出力してあげる

terminal
$ crontab -e

0 8 * * * /usr/local/bin/node 
[実行したいファイルの絶対パス] >> [出力したいログファイルの絶対パス] 2>&1
2
1
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
2
1