自动拉取目录下所有 git 项目的最新仓库
学习的时候会遇到下载很多代码仓库,但不一定记得每天都会拉取最新的仓库,为此设立一个自动拉取项目仓库代码的程序。
找到 git 项目,并拉取最新仓库
pull_all_gitproject.sh
#!/bin/bash
# 查找给定文件夹下的所有文件夹
# 通过参数传递需要查找仓库的目录, 方法1
find $1 -type d > all_folders.txt
# 可以做这里自己设置固定的,也可以通过参数进行设置,方法2
#find /home/xx/open -type d > all_folders.txt
# 当前路径
cur_dir=$(pwd)
# 先将所有文件夹写入到一个临时文件 all_folders.txt 中
cat all_folders.txt | while read line
do
# 根据末尾是否为 .git 判断是否为 git 仓库
suffix="${line:0-4}"
if [ "${suffix}" == ".git" ]
then
# 取项目路径
par_folder=${line%/*}
# 切换到项目路径
cd ${par_folder}
echo "In folder: ${par_folder}"
echo "git pull origin to ${par_folder}"
git remote -v
git pull origin
echo "..."
echo "done!"
echo ""
# 返回脚本路径
cd ${cur_dir}
fi
done
手动执行脚本
# 内部设定了目录,方法2
bash pull_all_gitproject.sh
# 参数设定目录,方法1
bash pull_all_gitproject.sh /home/xx/open
自动定时脚本
crontab -u //设定特定用户的定时服务
crontab -l //列出当前用户定时服务内容
crontab -r //删除当前用户的定时服务
crontab -e //编辑当前用户的定时服务
设定一个每天 13:00 定时拉取 git 代码的定时任务
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
# You can also override PATH, but by default, newer versions inherit it from the environment
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
输入 crontab -e
进入到编辑环境;将以下代码写入
0 13 * * * /home/xx/pull_all_gitproject.sh /home/xx/open
xx
代表的电脑的用户名,自己替换;
crontab -l
查看定时任务