一般若要某 user 定時執行某件事情, 就以那個 user 的帳號執行 crontab -e 就可以建立定時要執行的事情. 注意: 不要直接 edit crontab 檔案, 而是執行 crontab -e.
For Ubuntu
Method A: crontab -e
1. as "user" login
2. crontab -e
3. # m h dom mon dow command
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae
//Logs/git/git-"$(date +\%F_\%T)".log 2>&1
Method B: edit crontab file. This method doesn't work for CentOS.
1. as root
2. vim /etc/crontab
3. as user "fae" to execute command
# m h dom mon dow command
0,20,40 * * * *
fae /home/fae/git_repo/git_pull.sh >> /home/fae/
/Logs/git/git-"$(date +\%F_\%T)".log 2>&1
4. restart cron service
sudo service cron reload
or
/etc/init.d/cron reload
Method C: sudo crontab -e
1. as "user" login
2. sudo crontab -e
3. # m h dom mon dow command
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae/Logs/git/git-"$(date +\%F_\%T)".log 2>&1
10 3 * * 1-5 /home/fae/Tools/trim/trim_ssd.sh >> /home/fae/Logs/trim/trim-"$(date +\%F_\%T)".log 2>&1
觀察 crontab 執行結果
1. sudo cat /var/log/syslog | grep CRON
2. cat
log.txt
Solved CRON no MTA install
message ---- "CRON[8380]: (CRON) info (No MTA installed, discarding output)"
sudo apt-get install postfix
Example: git_pull.sh
#! /bin/bash
echo -e " "
echo -e "###############################################################"
echo -e "# SYNC start" `date +%Y_%m%d_%H:%M:%S`
echo -e " ---- git pull jmt_driver ---- "
cd /home/fae/git_repo/jmt_driver
git reset --hard HEAD
git pull
echo -e " ---- git pull jmt_docs ---- "
cd ../jmt_docs
git reset --hard HEAD
git pull
Example: crontab -e
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae/pull_log/git_pull_log.txt
For CentOS
Method A: crontab -e
1. as "user" login
2. crontab -e
3. # m h dom mon dow command
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae
//Logs/git/git-"$(date +\%F_\%T)".log 2>&1
Method B: edit crontab file. This method doesn't work for CentOS.
1. as root
2. vim /etc/crontab
3. as user "fae" to execute command
# m h dom mon dow command
0,20,40 * * * *
fae /home/fae/git_repo/git_pull.sh >> /home/fae/
/Logs/git/git-"$(date +\%F_\%T)".log 2>&1
4. restart cron service
sudo service cron reload
or
/etc/init.d/cron reload
Method C: sudo crontab -e
1. as "user" login
2. sudo crontab -e
3. # m h dom mon dow command
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae
/Logs/git/git-"$(date +\%F_\%T)".log 2>&1
10 3 * * 1-5 /home/fae/Tools/trim/trim_ssd.sh >> /home/fae
/Logs/trim/trim-"$(date +\%F_\%T)".log 2>&1
Crontab tack effect
For Ubuntu
sudo service cron reload
or
/etc/init.d/cron reload
For CentOS
sudo systemctl start crond.service
觀察 crontab 執行結果
1. sudo cat /var/log/
cron | grep -i corn
2. cat
log.txt
Example: git_pull.sh
#! /bin/bash
echo -e " "
echo -e "###############################################################"
echo -e "# SYNC start" `date +%Y_%m%d_%H:%M:%S`
echo -e " ---- git pull jmt_driver ---- "
cd /home/fae/git_repo/jmt_driver
git reset --hard HEAD
git pull
echo -e " ---- git pull jmt_docs ---- "
cd ../jmt_docs
git reset --hard HEAD
git pull
Example: trim_ssd.sh
#! /bin/bash
sudo fstrim -v /ssd1
sudo fstrim -v /ssd2
Example: crontab -e
0,20,40 * * * * /home/fae/git_repo/git_pull.sh >> /home/fae/pull_log/git_pull_log.txt