Neel Gajjar
Neel Gajjar

Reputation: 9

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

I am trying to run a script for git auto push when we shut down the PC. But my script and service file only execute the commit command and after that, the network goes down. So, it does not execute the push command.

How can I complete the execution of the script before the network goes down?

My service file:

[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

My script:

#!/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:[email protected]/username/Basics.git --all

Can anyone help me to figure out this issue?

Upvotes: 1

Views: 633

Answers (1)

VonC
VonC

Reputation: 1324278

after that, the network goes down

Actually, the network might very well be down before your service starts any Git command.

Check first if adding to your service definition After=networking.service would help.
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.

Upvotes: 2

Related Questions