运行 PC 上的 shell 脚本在网络仍然存在时关闭或设置网络延迟以在 ubuntu 中执行脚本

Run a shell script on PC shutdown while network is still up or set delay in network to execute script in ubuntu

我正在尝试 运行 一个脚本,以便在我们关闭 PC 时 git 自动推送。但是我的脚本和服务文件只执行提交命令,之后网络就崩溃了。所以,它不执行推送命令。

如何在网络中断之前完成脚本的执行?

我的服务档案:

[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

谁能帮我解决这个问题?

after that, the network goes down

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

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

After= not only declares that your service is started by the networking service, it also is declaring that the services should be stopped in the inverse order-- before networking is shut down.