V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
Fffys
V2EX  ›  git

备份到 github,直接输入可以但 cron 没成功

  •  
  •   Fffys · 14 天前 · 1267 次点击

    MAC M1 ,zsh cron 配置的内容为:* * * * * /bin/zsh /Users/macbook/back_up.sh > /Users/macbook/cron_log_ob2.txt 2>&1

    cron_log_ob2.txt的内容为:

    On branch main
    Your branch is up to date with 'origin/main'.
    
    nothing to commit, working tree clean
    fatal: could not read Username for 'https://github.com': Device not configured
    

    要执行的脚本的内容为:

    cd Documents/obsidian
    git add .
    git add -A
    git commit -a -m "backup from mac m1 using crontab"
    git push origin main -f
    

    为什么会显示 fatal: could not read Username for 'https://github.com': Device not configured 呢?如果直接在终端执行 /bin/zsh /Users/macbook/back_up.sh > /Users/macbook/cron_log_ob2.txt 2>&1 那么cron_log_ob2.txt的内容就一切正常,问题出在哪儿?

    7 条回复    2024-05-05 22:02:17 +08:00
    aloxaf
        2
    aloxaf  
       14 天前
    cron 默认应该是 root 执行的,你得切换到当前用户,这样才读的现有的 .gitconfig
    话说 obsidian 的 git 同步应该有插件吧
    Fffys
        3
    Fffys  
    OP
       14 天前
    @aloxaf 原来有同步插件啊!谢谢!我去瞅瞅! obsidian 的文件夹的用户是 macbook ,设置 crontab 的任务时也是为这个用户设置的:`sudo crontab -e -u macbook` 但是还是会有上面的错误!为什么呢……?没有其它用户了,终端用的是默认的 zsh ,你说的切换到当前用户是指?
    hefish
        4
    hefish  
       14 天前
    这是没有 tty 的原因吧。。。
    samnya
        5
    samnya  
       14 天前
    唔,虽然你设置 crontab 以那个用户运行,但可能还是获取不到$HOME 之类环境变量的,就获取不到你的 git 登录信息了。
    然后它要求你输入用户名和密码登录,因为是 crontab 里面运行的,要求输入的时候就报错了。

    如果只是解决上面这个问题的话,应该可以把 git 改成 ssh 登录,然后指定一个不需要密码的私钥
    skiy
        6
    skiy  
       12 天前
    直接用 ssh 方式就行了啊。用 https 方式,还得设置一下 .gitconfig
    julyclyde
        7
    julyclyde  
       12 天前
    @aloxaf 从他的 crontab 没有用户名字段来看,应该是用户级 crontab ,而不是系统级 crontab ,这个是按所属用户的身份来执行的,而不是“默认 root”

    @samnya 不至于读不到 HOME 吧
    man 5 crontab 明确指出:
    Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab´s owner. HOME and SHELL can be overridden by settings in the crontab; LOGNAME can
    not.

    而且他是通过 shell 调脚本的,shell 也会处理这些内容吧?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2582 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:56 · PVG 10:56 · LAX 19:56 · JFK 22:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.