【问题标题】:Run a shell script on PC shutdown while network is still up or set delay in network to execute script in ubuntu在网络仍然正常时在 PC 关闭时运行 shell 脚本或在网络中设置延迟以在 ubuntu 中执行脚本
【发布时间】:2021-12-31 07:16:24
【问题描述】:

当我们关闭 PC 时,我正在尝试运行 git auto push 的脚本。但是我的脚本和服务文件只执行了提交命令,之后网络就断了。所以,它不执行推送命令。

如何在网络不通之前完成脚本的执行?

我的服务文件:

[Unit]
Description=It will auto commit and push using auto-commit-push-script
DefaultDependencies=no
Wants=network-online.target
After=network-online.target
Before=halt.target poweroff.target  shutdown.target

[Service]
Type=oneshot
# RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/user/git-auto-commit/git_auto_commit.sh
TimeoutStopSec=60

[Install]
WantedBy=halt.target poweroff.target shutdown.target

我的脚本:

#!/bin/bash

cd /home/user/Basics
username="xyz"
password="xyz"
git add .
git commit -m "Auto-Commit On PC shut down"
git push https://$username:$password@github.com/username/Basics.git --all

谁能帮我解决这个问题?

【问题讨论】:

  • 这实际上与 Git 没有任何关系 - 任何 网络操作都会有相同的问题,例如 scp、rsync、tar cf - dir | ssh backupsite savestuff 等等。不过,VonC 的回答很好。

标签: git ubuntu sh systemctl


【解决方案1】:

之后,网络中断

实际上,网络很可能在您的服务启动任何 Git 命令之前关闭。

首先检查是否将After=networking.service 添加到您的服务定义中会有帮助。
From here

After= 不仅声明了你的服务是由网络服务启动的,它还声明了服务应该以相反的顺序停止——网络关闭之前。 p>

【讨论】:

  • 感谢您的快速回复。让我试试这个。
  • 仍然存在同样的问题。它在 4 次中工作 3 次。 1次还是没有推送代码。
  • @NeelGajjar 尝试将您的foo.conf 放入/lib/systemd/system/foo.d/lib 中的那些应首先停止,而其他服务仍处于活动状态。
  • 我不明白你的意思。能不能说的详细点?
  • @NeelGajjar From freedesktop.org/software/systemd/man/systemd.unit.html: "除了/etc/systemd/system,系统服务的".d/"目录可以放在/usr/lib/systemd/ system 或 /run/systemd/system 目录。/etc/ 中的插入文件优先于 /run/ 中的文件,而 /run/ 中的文件又优先于 /usr/lib/ 中的文件。” 想法是放置一个 /usr/lib/system/foo.d/foo.conf在优先级最低的文件夹中,意思是:它应该是第一个停止,其他(包括网络)之前停止
猜你喜欢
  • 2013-03-31
  • 2014-08-01
  • 2021-01-18
  • 2014-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多