我有一个简单的 bash 脚本,它调用 github 上的 git 存储库(/home/user/simple_git.sh):
#!/bin/bash
# Change to the Git repoistory
cd /home/user/git/a_git_repo
remote=$(
git ls-remote -h origin master |
awk '{print $1}'
)
local=$(
git rev-parse HEAD
)
printf "Local : %s\nRemote: %s\n" $local $remote
它提供以下输出:
Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
Remote: a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
git 身份验证是通过 ssh 密钥完成的 - 以下是我的 .bashrc
# ssh
eval `ssh-agent -s`
ssh-add
该脚本作为用户以及使用 sudo (通过保留用户环境)运行得很好,即。
~/.simple_git.sh
or
sudo -E ~/simple_git.sh
但是,我还没有找到将脚本作为服务运行的方法(/etc/systemd/user/simple_git.service 或 /etc/systemd/system/simple_git.service)
[Unit]
Description=TestScript
[Service]
Type=simple
ExecStart=/home/user/simple_git.sh
我尝试使用 --user 选项运行 systemctl 命令以及修改 visudo 以包含该行
Defaults env_keep += SSH_AUTH_SOCK
但无济于事。每次我检查工作的状态时:
Feb 21 23:16:00 alarmpi systemd[484]: Started TestScript.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Permission denied (publickey).
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: fatal: Could not read from remote repository.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Please make sure you have the correct access rights
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: and the repository exists.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b