Скрипт выключения Systemd работает неправильно

Скрипт выключения Systemd работает неправильно

Я пишу службу Systemd (написано в соответствии с указаниями:Как запустить скрипт с помощью systemd прямо перед выключением?) для синхронизации, шифрования и отправки некоторых файлов при каждой перезагрузке или выключении. Запуск скрипта вручную работает безупречно, но если разрешить Systemd запустить его, возникает несколько проблем:

  1. Кажется, скрипт запускается не по порядку ( git addпоявляется первым, хотя должен быть последним)
  2. В связи с проблемой, описанной выше, скрипт игнорирует --git-dirи, по-видимому, пытается добавить всю файловую систему в репозиторий git.

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

решение1

Нужно добавить --working-tree=/dirв команду git

ссылка:git --git-dir не работает как ожидалось

Связанный контент