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

35 天前
 Fffys

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的内容就一切正常,问题出在哪儿?

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

如果只是解决上面这个问题的话,应该可以把 git 改成 ssh 登录,然后指定一个不需要密码的私钥
skiy
33 天前
直接用 ssh 方式就行了啊。用 https 方式,还得设置一下 .gitconfig
julyclyde
33 天前
@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 也会处理这些内容吧?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1037607

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX