定时脚本自动自动将文件push到git

本文介绍了如何在Bash脚本中执行Python程序,监控仓库并记录IP地址和时间戳,同时使用Crontab定制定时任务。脚本会将数据写入文件并通过Git进行版本控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写脚本
绝对路径
环境注意
写python,bash脚本执行调用 py程序
定制crontab -e
日志要指定输入文件中

项目地址
https://gitee.com/stdev_1/sshpi10/
bash脚本
#!/bin/bash

设置要监控的仓库路径

#path=~/github/
#watch_dir=“/home/pi/gittest/ipset/sshpi10”

python3 /home/pi/gittest/ipset/sshpi10/readip.py
cd /home/pi/gittest/ipset/sshpi10
git pull
git add .
git commit -m ‘ip’
git push -u origin master
python脚本执行的主体功能,
要将ip和时间戳,写入本地文件,文件推送 git
import datetime
import os

获取本地 IP 地址

local_ip = os.popen(“ifconfig”).read()#.splitlines()[2].split(" ")[-1]

获取当前时间

current_time = datetime.datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)

将本地 IP 地址和当前时间写入到 1.txt 文件

with open(“/home/pi/gittest/ipset/sshpi10/1.txt”, “w”) as f:
f.write(f"{local_ip}\n{current_time}\n")
crontab -e
定制任务

          • /bin/bash /home/pi/gittest/ipset/sshpi10/apush.sh >> /home/pi/bashset.log 2>&1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世修行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值