literate-dotfiles/dotfiles/systemd.org

386 lines
8.8 KiB
Org Mode

#+TITLE: Systemd services and timers
:DRAWER:
#+STARTUP: overview
#+LANGUAGE: en
#+EMAIL: dehaeze.thomas@gmail.com
#+AUTHOR: Dehaeze Thomas
#+HTML_LINK_HOME: ./index.html
#+HTML_LINK_UP: ./index.html
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/readtheorg.css"/>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.stickytableheaders.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/readtheorg.js"></script>
:END:
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
* Pass Git
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/passgit.service
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync Passwords every day
RefuseManualStart=no
RefuseManualStop=yes
[Service]
Type=oneshot
ExecStart=%h/scripts/pass_git_sync.sh
#+END_SRC
** Timer
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/passgit.timer
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync Passwords every day
RefuseManualStart=no
RefuseManualStop=no
Wants=network-online.target
After=network-online.target
[Timer]
OnCalendar=*-*-* 16:00:00
Persistent=true
Unit=passgit.service
[Install]
WantedBy=default.target
#+END_SRC
** Script
:PROPERTIES:
:header-args: :tangle ~/scripts/pass_git_sync.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
pass git pull --rebase && pass git push
#+end_src
* Buku Git
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/bukugit.service
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync Bookmarks every day
RefuseManualStart=no
RefuseManualStop=yes
[Service]
Type=oneshot
ExecStart=%h/scripts/buku_git_push.sh
#+END_SRC
** Timer
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/bukugit.timer
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync All Mails every x hours
RefuseManualStart=no
RefuseManualStop=no
Wants=network-online.target
After=network-online.target
[Timer]
OnCalendar=*-*-* 16:00:00
Persistent=true
Unit=bukugit.service
[Install]
WantedBy=default.target
#+END_SRC
** Script
:PROPERTIES:
:header-args: :tangle ~/scripts/buku_git_push.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
cd ~/.local/share/buku/
if [[ ! -z $(git status -s bookmarks.db) ]]
then
git add bookmarks.db
git commit -m "Changed bookmarks - $(date +%F)"
git push
exit
fi
#+end_src
* Check mail
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/checkmail.service
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Check new mails
RefuseManualStart=no
RefuseManualStop=yes
[Service]
Type=oneshot
ExecStart=%h/scripts/checkmail.sh -q
#+END_SRC
** Timer
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/checkmail.timer
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Check Mail every x minutes
RefuseManualStart=no
RefuseManualStop=no
Wants=network-online.target
After=network-online.target
Requires=checkmail.service
[Timer]
Persistent=false
OnBootSec=2min
OnUnitActiveSec=5min
AccuracySec=2min
Unit=checkmail.service
[Install]
WantedBy=default.target
#+END_SRC
** Script
:PROPERTIES:
:header-args: :tangle ~/scripts/checkmail.sh
:header-args+: :comments both :mkdirp yes
:header-args+: :shebang "#!/usr/bin/env bash"
:END:
#+begin_src bash
while [ -n "$1" ]; do # while loop starts
case "$1" in
-a) opt_all='--all' ;; # Check All inboxes
-v) opt_verbose='--verbose' ;; # Verbose
-q) opt_quiet='--quiet' ;; # Quiet
*) echo "Option $1 not recognized" ;; # In case you typed a different option
esac
shift
done
# =============================================================
# Count number of mails
gmail_old="$(ls ~/.mail/gmail/Inbox/new | wc -l)"
esrf_old="$(ls ~/.mail/esrf/Inbox/new | wc -l)"
ulg_old="$(ls ~/.mail/ulg/Inbox/new | wc -l)"
uliege_old="$(ls ~/.mail/uliege/Inbox/new | wc -l)"
# =============================================================
# =============================================================
# Retreive mails
mbsync $opt_all $opt_verbose gmail-Home esrf-Home ulg-Home uliege-Home
# =============================================================
# =============================================================
# Count number of mails
gmail_new="$(ls ~/.mail/gmail/Inbox/new | wc -l)"
esrf_new="$(ls ~/.mail/esrf/Inbox/new | wc -l)"
ulg_new="$(ls ~/.mail/ulg/Inbox/new | wc -l)"
uliege_new="$(ls ~/.mail/uliege/Inbox/new | wc -l)"
# =============================================================
# =============================================================
# Total Number of new mails since last checking
new="$(($gmail_new+$esrf_new+$ulg_new+$uliege_new))"
old="$(($gmail_old+$esrf_old+$ulg_old+$uliege_old))"
# =============================================================
# =============================================================
# Notification
if [ "$new" -gt 0 ]; then
dunstify --replace=98465 'New mail' "$esrf_new$gmail_new$ulg_new$uliege_new"
fi
# =============================================================
# =============================================================
# Indexation and Tags
# mu update
mu index --maildir=~/.mail $opt_verbose $opt_quiet
# Update on Emacs
# if [ "$(($new-$old))" -gt 0 ]; then
# emacsclient --eval '(mu4e-update-index)';
# fi
# =============================================================
#+end_src
* Sync mail
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/syncmail.service
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync all mails
RefuseManualStart=no
RefuseManualStop=yes
[Service]
Type=oneshot
ExecStart=%h/scripts/checkmail.sh -a -q
#+END_SRC
** Timer
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/syncmail.timer
:header-args+: :comments both :mkdirp yes
:END:
#+BEGIN_SRC conf
[Unit]
Description=Sync All Mails every x hours
RefuseManualStart=no
RefuseManualStop=no
Wants=network-online.target
After=network-online.target
Requires=syncmail.service
[Timer]
Persistent=false
OnBootSec=30min
OnUnitActiveSec=300min
AccuracySec=10min
Unit=syncmail.service
[Install]
WantedBy=default.target
#+END_SRC
* Vdirsyncer
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/vdirsyncer.service
:header-args+: :comments both :mkdirp yes
:END:
#+begin_src conf
[Unit]
Description=Synchronize calendars and contacts
Documentation=https://vdirsyncer.readthedocs.org/
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/bin/vdirsyncer --verbosity "ERROR" sync
Type=oneshot
#+end_src
** Timer
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/vdirsyncer.timer
:header-args+: :comments both :mkdirp yes
:END:
#+begin_src conf
[Unit]
Description=Synchronize vdirs
[Timer]
OnBootSec=5m
OnUnitActiveSec=15m
AccuracySec=5m
[Install]
WantedBy=timers.target
#+end_src
* SSH Agent
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/ssh-agent.service
:header-args+: :comments both :mkdirp yes
:END:
#+begin_src conf
[Unit]
Description=SSH key agent
[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
[Install]
WantedBy=default.target
#+end_src
* Powertop
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/powertop.service
:header-args+: :comments both :mkdirp yes
:END:
#+begin_src conf
[Unit]
Description=Powertop tunings
[Service]
ExecStart=/usr/bin/powertop --auto-tune
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
#+end_src
* Mopidy
** Service
:PROPERTIES:
:header-args: :tangle ~/.config/systemd/user/mopidy.service
:header-args+: :comments both :mkdirp yes
:END:
#+begin_src conf
[Unit]
Description=Mopidy music server
After=avahi-daemon.service
After=dbus.service
After=network.target
After=nss-lookup.target
After=pulseaudio.service
After=remote-fs.target
After=sound.target
[Service]
ExecStart=/usr/bin/mopidy --config ~/.config/mopidy/mopidy.conf
[Install]
WantedBy=multi-user.target
#+end_src