linux cron实战:定时github

相关文章:GitHub第一步:生成ssh keys

最近由于需要使用实验室的一台高性能工作站来跑程序,ssh进行远程登录,但是有时候电脑重启后,ip发生变化,于是在思考用什么办法来存储这台电脑的ip地址。

刚开始的想法是把ip地址直接上传到这个博客,定时更新。后来想想,既然是自己自定义cron任务,能不能上传到github呢?这样就不用浪费博客的流量了,也是进行了下面的实验:

一、编写shell脚本

首先是确定我要如何更新,更新一些什么信息:

最简单的是,需要更新ip和时间,于是我写了个sh脚本,使用date和ifconfig来获取时间和ip信息,最后将两个文件合并,如下:

#use the 'date' command to get the date, and output to the 'tmp1' file
#使用date命令获取时间,并重点向输出到文件tmp1
date > tmp1

#use the 'ifconfig' command to get ip information(just get the ip address include '172',such as 172.168.12.12), and outpt to the 'tmp2' file
#使用ifconfig获取172字段的信息,因为局域网的字段为172开头,避免泄漏不必要的信息,只grep了这部分,同样重定向输出到tmp2
ifconfig | grep 172 > tmp2

#use the 'cat' command to merger 2 files
#使用cat 命令合并两个文件
cat tmp1 tmp2 > result

其中,我们的局域网网段是172开头的,所以直接grep 172。

接着,我尝试在sh脚本里面加上git的操作,整个cron.sh脚本如下:

#!/bin/bash

#open the directory
#打开cron目录
cd ~/cron

#to avoid the new version changed by other users,git pull first
#避免其他用户更新了目录,先进行git pull到endIdx
git pull

#open the directory again
#再次打开目录
cd ~/cron

#use the 'date' command to get the date, and output to the 'tmp1' file
#使用date命令获取时间,并重点向输出到文件tmp1
date > tmp1

#use the 'ifconfig' command to get ip information(just get the ip address include '172',such as 172.168.12.12), and outpt to the 'tmp2' file
#使用ifconfig获取172字段的信息,因为局域网的字段为172开头,避免泄漏不必要的信息,只grep了这部分,同样重定向输出到tmp2
ifconfig | grep 172 > tmp2

#use the 'cat' command to merger 2 files
#使用cat 命令合并两个文件
cat tmp1 tmp2 > result

#add cron.sh and the result file to the git cache
#把cron.sh和result文件添加到git缓冲区
git add cron.sh
git add result

#git commit the change
#提交更改
git commit -m "refresh"

#push to the github
#最后,上传到github
git push

把cron.sh的权限更改为可执行:

chmod +x cron.sh

然后运行:

./cron.sh

发现能够正常工作。

二、添加cron任务

首先我们可以通过

crontab -l

列出当前的crontab文件内容,即看看是否有cron任务。

接着,我们通过:

crontab -e

命令编辑crontab 文件,添加以下内容:

# crontab -l #list the cron jobs
# crontab -e #to edit the cron
# add these text to the crontab,then save and quit
# it will run cron.sh every 15 minutes
# :wq
# it will be better to restart cron service
# ubuntu:  sudo /etc/init.d/cron restart
# centos:  sudo /sbin/service crond restart

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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
*/15 *  *  *  *  /bin/bash ~/cron/cron.sh

其中,关键在于最后一行:

*/15 *  *  *  *  /bin/bash ~/cron/cron.sh

意思是每15分钟执行一次~/cron/cron.sh

保存退出后,为了保险起见,最好重启一下cron服务:

# ubuntu:  
sudo /etc/init.d/cron restart
# centos:  
sudo /sbin/service crond restart

注意ubuntu和centos的命令有些许不同,至此,我们的自动github更新ip程序完成!

通过这个程序,我们还可以定时github其他的信息,既能延长github的最长contributions时间,又可以免费使用github的服务器帮我们存储信息。

项目网址:

https://github.com/siukwan/cron

在日夜运行的实验室服务器ndcRobot:

https://github.com/ndcRobot/cron

发表评论

电子邮件地址不会被公开。 必填项已用*标注