linux cron 博客,linux cron实战:定时github

最近由于需要使用实验室的一台高性能工作站来跑程序,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的服务器帮我们存储信息。

项目网址:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值