Systemd-Shutdown-Skript wird falsch ausgeführt

Systemd-Shutdown-Skript wird falsch ausgeführt

Ich schreibe einen Systemd-Dienst (geschrieben wie angegeben:Wie führe ich direkt vor dem Herunterfahren ein Skript mit systemd aus?), um bei jedem Neustart oder Herunterfahren einige Dateien zu synchronisieren, zu verschlüsseln und zu übertragen. Das manuelle Ausführen des Skripts funktioniert einwandfrei, aber wenn ich es Systemd überlasse, gibt es ein paar Probleme:

  1. Es scheint, als würde das Skript in der falschen Reihenfolge ausgeführt ( git addes wird zuerst angezeigt, obwohl es fast zuletzt erscheinen sollte).
  2. Im Zusammenhang mit dem oben genannten Problem ignoriert das Skript --git-dirdas gesamte Dateisystem und versucht anscheinend, es zum Git-Repository hinzuzufügen

sync.service

[Unit]
Description=Sync, encrypt, and push important files

[Service]
Type=oneshot
User=shane
RemainAfterExit=true
ExecStop=/home/shane/bin/sync.py

[Install]
WantedBy=multi-user.target

sync.py

#!/usr/bin/python

import os
import shutil
import subprocess
import datetime

def sync_config():
    source_root = '/home/shane'
    destination_root = '/home/shane/sync/public/config'
    files = [
            '.config/awesome',
            '.config/cmus/playlists',
            '.config/cmus/autosave',
            '.config/cmus/shane.theme',
            '.vim/autoload',
            '.vim/colors',
            'bin',
            '.bashrc',
            '.vimrc',
            ]

    print('Syncing configuration files')
# clear out old config
    for file in os.listdir(destination_root):
        path = os.path.join(destination_root, file)
        if os.path.isfile(path):
            os.unlink(path)
        elif os.path.isdir(path):
            shutil.rmtree(path)
        print('    removed ' + path)

# copy files to config
    for file in files:
        source = os.path.join(source_root, file)
        destination = os.path.join(destination_root, file)
        if os.path.isfile(source):
            shutil.copy(source, destination)
        else:
            shutil.copytree(source, destination)
        print('    copied ' + source + ' to ' + destination)

def encrypt_private():
    root = '/home/shane/sync'
    private = 'private'
    passfile = '/home/shane/sync/private/gpgpassphrase'
    tar = os.path.join(root, private) + '.tar.gz'
    gpg = tar + '.gpg'

    print('Encrypting private folder')
    print('    removing old')
    if os.path.isfile(gpg):
        os.remove(gpg)

    print('    compressing')
    subprocess.run(['tar', '-czf', tar, '-C', root, private])

    print('    encrypting')
    subprocess.run(['gpg2', '--batch', '--passphrase-file', passfile, '-c', tar])
    os.remove(tar)

def push_sync():
    git = '/home/shane/sync/.git'
    time = 'synced ' + str(datetime.datetime.now())

    print('Pushing synced files')
    subprocess.run(['git', '--git-dir=' + git, 'add', '-A'])
    subprocess.run(['git', '--git-dir=' + git, 'commit', '-m', time])
    subprocess.run(['git', '--git-dir=' + git, 'push', 'bitbucket', 'master'])


sync_config()
encrypt_private()
push_sync()

# journalctl -u sync

-- Logs begin at Thu 2019-06-27 00:13:11 MST, end at Sun 2019-06-30 22:10:03 MST. --
Jun 30 22:05:03 arch systemd[1]: Started Sync, encrypt, and push important files.
Jun 30 22:05:06 arch systemd[1]: Stopping Sync, encrypt, and push important files...
Jun 30 22:05:07 arch sync.py[16940]: warning: could not open directory 'proc/tty/driver/': Permission denied
Jun 30 22:05:07 arch sync.py[16940]: warning: could not open directory 'proc/1/task/1/fd/': Permission denied
Jun 30 22:05:07 arch sync.py[16940]: warning: could not open directory 'proc/1/task/1/fdinfo/': Permission denied
...
Jun 30 22:05:13 arch sync.py[16940]: error: open("boot/grub/grub.cfg"): Permission denied
Jun 30 22:05:13 arch sync.py[16940]: error: unable to index file 'boot/grub/grub.cfg'
Jun 30 22:05:13 arch sync.py[16940]: fatal: adding files failed
Jun 30 22:05:13 arch sync.py[16940]: warning: could not open directory 'root/': Permission denied
Jun 30 22:05:13 arch sync.py[16940]: warning: could not open directory 'lost+found/': Permission denied
Jun 30 22:05:13 arch sync.py[16940]: On branch master
Jun 30 22:05:13 arch sync.py[16940]: Changes not staged for commit:
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    .gitignore
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    private.tar.gz.gpg
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.bashrc
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/awesome/rc.lua
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/awesome/themes/shane/theme.lua
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/awesome/themes/shane/wallpaper.jpg
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/cmus/autosave
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/cmus/playlists/all
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/cmus/playlists/top
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.config/cmus/shane.theme
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.vim/autoload/plug.vim
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.vim/autoload/plug.vim.old
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.vim/colors/molokai.vim
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/.vimrc
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/bin/sync.py
Jun 30 22:05:13 arch sync.py[16940]:         deleted:    public/config/bin/sync.service
Jun 30 22:05:13 arch sync.py[16940]: Untracked files:
Jun 30 22:05:13 arch sync.py[16940]:         bin
Jun 30 22:05:13 arch sync.py[16940]:         boot/
Jun 30 22:05:13 arch sync.py[16940]:         dev/
Jun 30 22:05:13 arch sync.py[16940]:         etc/
Jun 30 22:05:13 arch sync.py[16940]:         home/
Jun 30 22:05:13 arch sync.py[16940]:         lib
Jun 30 22:05:13 arch sync.py[16940]:         lib64
Jun 30 22:05:13 arch sync.py[16940]:         proc/
Jun 30 22:05:13 arch sync.py[16940]:         run/
Jun 30 22:05:13 arch sync.py[16940]:         sbin
Jun 30 22:05:13 arch sync.py[16940]:         sys/
Jun 30 22:05:13 arch sync.py[16940]:         tmp/
Jun 30 22:05:13 arch sync.py[16940]:         usr/
Jun 30 22:05:13 arch sync.py[16940]:         var/
Jun 30 22:05:13 arch sync.py[16940]: no changes added to commit
Jun 30 22:05:14 arch sync.py[16940]: Everything up-to-date
Jun 30 22:05:14 arch sync.py[16940]: Syncing configuration files
Jun 30 22:05:14 arch sync.py[16940]:     removed /home/shane/sync/public/config/bin
Jun 30 22:05:14 arch sync.py[16940]:     removed /home/shane/sync/public/config/.bashrc
Jun 30 22:05:14 arch sync.py[16940]:     removed /home/shane/sync/public/config/.config
Jun 30 22:05:14 arch sync.py[16940]:     removed /home/shane/sync/public/config/.vim
Jun 30 22:05:14 arch sync.py[16940]:     removed /home/shane/sync/public/config/.vimrc
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.config/awesome to /home/shane/sync/public/config/.config/awesome
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.config/cmus/playlists to /home/shane/sync/public/config/.config/cmus/playlists
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.config/cmus/autosave to /home/shane/sync/public/config/.config/cmus/autosave
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.config/cmus/shane.theme to /home/shane/sync/public/config/.config/cmus/shane.theme
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.vim/autoload to /home/shane/sync/public/config/.vim/autoload
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.vim/colors to /home/shane/sync/public/config/.vim/colors
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/bin to /home/shane/sync/public/config/bin
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.bashrc to /home/shane/sync/public/config/.bashrc
Jun 30 22:05:14 arch sync.py[16940]:     copied /home/shane/.vimrc to /home/shane/sync/public/config/.vimrc
Jun 30 22:05:14 arch sync.py[16940]: Encrypting private folder
Jun 30 22:05:14 arch sync.py[16940]:     removing old
Jun 30 22:05:14 arch sync.py[16940]:     compressing
Jun 30 22:05:14 arch sync.py[16940]:     encrypting
Jun 30 22:05:14 arch sync.py[16940]: Pushing synced files
Jun 30 22:05:14 arch systemd[1]: sync.service: Succeeded.
Jun 30 22:05:14 arch systemd[1]: Stopped Sync, encrypt, and push important files.
-- Reboot --
Jun 30 22:05:57 arch systemd[1]: Started Sync, encrypt, and push important files.

$ ~/bin/sync.py

[master f1e3c29] synced 2019-06-30 22:18:33.882881
 1 file changed, 0 insertions(+), 0 deletions(-)
 rewrite private.tar.gz.gpg (93%)
Syncing configuration files
    removed /home/shane/sync/public/config/bin
    removed /home/shane/sync/public/config/.bashrc
    removed /home/shane/sync/public/config/.config
    removed /home/shane/sync/public/config/.vim
    removed /home/shane/sync/public/config/.vimrc
    copied /home/shane/.config/awesome to /home/shane/sync/public/config/.config/awesome
    copied /home/shane/.config/cmus/playlists to /home/shane/sync/public/config/.config/cmus/playlists
    copied /home/shane/.config/cmus/autosave to /home/shane/sync/public/config/.config/cmus/autosave
    copied /home/shane/.config/cmus/shane.theme to /home/shane/sync/public/config/.config/cmus/shane.theme
    copied /home/shane/.vim/autoload to /home/shane/sync/public/config/.vim/autoload
    copied /home/shane/.vim/colors to /home/shane/sync/public/config/.vim/colors
    copied /home/shane/bin to /home/shane/sync/public/config/bin
    copied /home/shane/.bashrc to /home/shane/sync/public/config/.bashrc
    copied /home/shane/.vimrc to /home/shane/sync/public/config/.vimrc
Encrypting private folder
    removing old
    compressing
    encrypting
Pushing synced files
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 469.08 KiB | 12.03 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://bitbucket.org/*****/sync.git
   7ba1c3c..f1e3c29  master -> master

Antwort1

Muss --working-tree=/dirzum Git-Befehl hinzugefügt werden

Referenz:git --git-dir funktioniert nicht wie erwartet

verwandte Informationen