crontab
syntax
sudo crontab -e
1 2
| */5 * * * * /home/user/git/repo/python-demo/run.sh
|
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
crontab FAQs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| sudo crontab -e sudo crontab -l
sudo vim /etc/rsyslog.d/50-default.conf cron.* /var/log/cron.log
sudo service rsyslog restart
tail -f /var/log/cron.log
sudo crontab -e
* * * * * env > /home/kezunlin/env.log
LC_TIME=zh_CN.UTF-8
LC_MONETARY=zh_CN.UTF-8 LOGNAME=root
LC_ADDRESS=zh_CN.UTF-8 LANG=en_US.UTF-8 LC_TELEPHONE=zh_CN.UTF-8 LC_NAME=zh_CN.UTF-8 SHELL=/bin/sh LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=zh_CN.UTF-8 PWD=/root
LC_NUMERIC=zh_CN.UTF-8 LC_PAPER=zh_CN.UTF-8
* * * * * sleep 5s && echo "yo" * * * * * env >> $HOME/env.log * * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo" */5 * * * * /home/ubuntu/scripts/run5.sh
*/1 * * * * $MYFB_ROOT/scripts/run5.sh */1 * * * * /home/ubuntu/project/scripts/run5.sh
|
crontab 终极方案
ENVS
vim /etc/environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
MYFB_ROOT=/home/kezunlin/git/repo/myproject/ PYTHONPATH=$PYHONPATH:$MYFB_ROOT../ PYTHON_BIN=/home/kezunlin/anaconda3/bin/python
MYFB_ROOT=/home/ubuntu/git/myproject/ PYTHONPATH=$PYHONPATH:$MYFB_ROOT../ PYTHON_BIN=/usr/bin/python3
MYFB_ROOT=/home/ubuntu/git/myproject/ PYTHONPATH=$PYHONPATH:$MYFB_ROOT../ PYTHON_BIN=/usr/bin/python3
|
crontab -e
1 2 3 4 5 6
|
* * * * * env > $MYFB_ROOT/scripts/env.log */5 * * * * $MYFB_ROOT/scripts/run5.sh */10 * * * * $MYFB_ROOT/scripts/run10.sh
|
Scripts
scripts/run5.sh
1 2 3 4 5 6
| #!/bin/sh . /etc/environment
cd $MYFB_ROOT $PYTHON_BIN run5.py >> log/log5.txt
|
scripts/run10.sh
1 2 3 4 5 6
| #!/bin/sh . /etc/environment
cd $MYFB_ROOT $PYTHON_BIN run10.py >> log/log10.txt
|
Reference
History