literate-dotfiles/dotfiles/systemd.org

11 KiB

Systemd services and timers

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

Pass Git

Service

  [Unit]
  Description=Sync Passwords every day
  RefuseManualStart=no
  RefuseManualStop=yes

  [Service]
  Type=oneshot
  ExecStart=%h/scripts/pass_git_sync.sh

Timer

  [Unit]
  Description=Sync Passwords every day
  RefuseManualStart=no
  RefuseManualStop=no
  Wants=network-online.target
  After=network.target network-online.target dbus.socket

  [Timer]
  OnCalendar=*-*-* 17:00:00
  Persistent=true
  Unit=passgit.service

  [Install]
  WantedBy=default.target

Script

  pass git pull --rebase && pass git push

Buku Git

Service

  [Unit]
  Description=Sync Bookmarks every day
  RefuseManualStart=no
  RefuseManualStop=yes

  [Service]
  Type=oneshot
  ExecStart=%h/scripts/buku_git_push.sh

Timer

  [Unit]
  Description=Sync All Mails every x hours
  RefuseManualStart=no
  RefuseManualStop=no
  Wants=network-online.target
  After=network.target network-online.target dbus.socket

  [Timer]
  OnCalendar=*-*-* 16:00:00
  Persistent=true
  Unit=bukugit.service

  [Install]
  WantedBy=default.target

Script

  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

Check mail

Service

  [Unit]
  Description=Check new mails
  RefuseManualStart=no
  RefuseManualStop=yes

  [Service]
  Type=oneshot
  ExecStart=%h/scripts/checkmail.sh -q -v

Timer

  [Unit]
  Description=Check Mail every x minutes
  RefuseManualStart=no
  RefuseManualStop=no
  Wants=network-online.target
  After=network.target network-online.target dbus.socket
  Requires=checkmail.service

  [Timer]
  Persistent=false
  OnBootSec=2min
  OnUnitActiveSec=5min
  AccuracySec=10s
  Unit=checkmail.service

  [Install]
  WantedBy=default.target

Script

  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
  # =============================================================

Sync mail

Service

  [Unit]
  Description=Sync all mails
  RefuseManualStart=no
  RefuseManualStop=yes

  [Service]
  Type=oneshot
  ExecStart=%h/scripts/checkmail.sh -a -q -v

Timer

  [Unit]
  Description=Sync All Mails every x hours
  RefuseManualStart=no
  RefuseManualStop=no
  Wants=network-online.target
  After=network.target network-online.target dbus.socket
  Requires=syncmail.service

  [Timer]
  Persistent=false
  OnBootSec=30min
  OnUnitActiveSec=300min
  AccuracySec=10s
  Unit=syncmail.service

  [Install]
  WantedBy=default.target

Vdirsyncer

Service

  [Unit]
  Description=Synchronize calendars and contacts
  Documentation=https://vdirsyncer.readthedocs.org/
  Wants=network-online.target
  After=network.target network-online.target

  [Service]
  ExecStart=/usr/bin/vdirsyncer sync
  Type=oneshot

Timer

  [Unit]
  Description=Synchronize vdirs

  [Timer]
  OnBootSec=5m
  OnUnitActiveSec=15m
  AccuracySec=5m

  [Install]
  WantedBy=timers.target

Ranger

Service

  [Unit]
  Description=Ranger using Tmux

  [Service]
  Type=forking
  ExecStart=/usr/bin/tmux -L ranger new -d -s ranger ranger
  ExecStop=/usr/bin/tmux -L ranger kill-session -t ranger
  Restart=always
  RestartSec=1

  [Install]
  WantedBy=multi-user.target

Weechat

Service

  [Unit]
  Description=A WeeChat client and relay service using Tmux
  Wants=network-online.target
  After=network.target network-online.target

  [Service]
  Type=forking
  ExecStart=/usr/bin/tmux -L weechat new -d -s weechat weechat
  ExecStop=/usr/bin/tmux -L weechat kill-session -t weechat
  Restart=always
  RestartSec=3

  [Install]
  WantedBy=multi-user.target

Neomutt

Service

  [Unit]
  Description=Neomutt - mail client

  [Service]
  Type=forking
  ExecStart=/usr/bin/tmux -L neomutt new -d -s neomutt neomutt
  ExecStop=/usr/bin/tmux -L neomutt kill-session -t neomutt
  Restart=always
  RestartSec=3

  [Install]
  WantedBy=multi-user.target

Powertop

Service

  [Unit]
  Description=Powertop tunings

  [Service]
  ExecStart=/usr/bin/powertop --auto-tune
  RemainAfterExit=true

  [Install]
  WantedBy=multi-user.target

NCMPCPP

Service

  [Unit]
  Description=Ncmpcpp - Music Player

  [Service]
  Type=forking
  ExecStart=/usr/bin/tmux -L ncmpcpp new -d -s ncmpcpp ncmpcpp
  ExecStop=/usr/bin/tmux -L ncmpcpp kill-session -t ncmpcpp
  Restart=always
  RestartSec=3

  [Install]
  WantedBy=multi-user.target

Newsboat

Service

  [Unit]
  Description=Newsboat - RSS reader

  [Service]
  Type=forking
  ExecStart=/usr/bin/tmux -L newsboat new -d -s newsboat newsboat
  ExecStop=/usr/bin/tmux -L newsboat kill-session -t newsboat
  Restart=always
  RestartSec=3

  [Install]
  WantedBy=multi-user.target

SSH Agent

Service

  [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

Mopidy

Service

  [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