Skip to content

Instantly share code, notes, and snippets.

@gwpl
Last active October 2, 2021 16:19
Show Gist options
  • Save gwpl/b80d97d90b5134293ef530c05c7976cc to your computer and use it in GitHub Desktop.
Save gwpl/b80d97d90b5134293ef530c05c7976cc to your computer and use it in GitHub Desktop.
systemd.timer example: rsync_userx_to_remotey.sh rsync script example with pidfile and trap for the purpose of https://goo.gl/rzTJD1
Copy&Paste of : https://goo.gl/rzTJD1
self-link (web): https://goo.gl/rzTJD1
self-link (doc): https://goo.gl/kZDBHC
# linux userX -> remotey (only over local network)
Notes from setting rsync systemd.timer .
systemd.timers
references:
https://www.freedesktop.org/software/systemd/man/systemd.timer.html
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
"RequiresMountsFor=/var/cache/man" example from /usr/lib/systemd/system/man-db.{service,timer} or https://utcc.utoronto.ca/~cks/space/blog/linux/SystemdAndBindMounts
https://www.freedesktop.org/software/systemd/man/systemd.service.html
https://wiki.archlinux.org/index.php/Systemd/Timers
https://wiki.archlinux.org/index.php/Systemd#Writing_unit_files
http://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/
https://coreos.com/os/docs/latest/scheduling-tasks-with-systemd-timers.html
https://seanmcgary.com/posts/how-to-use-systemd-timers
http://unix.stackexchange.com/a/138317/9689
https://gist.github.com/gwpl/b80d97d90b5134293ef530c05c7976cc
http://unix.stackexchange.com/a/206639/9689 - runing timer/service as user
Scripts:
https://gist.github.com/gwpl/b80d97d90b5134293ef530c05c7976cc
# variables
basename=rsync_to_remotey
#enable, start
systemctl enable "${basename}".timer
systemctl enable "${basename}".service
systemctl start "${basename}".timer
#status
systemctl -n 50 status "${basename}".{timer,service}
journalctl -f -u "${basename}".service
#stop, disable
systemctl stop "${basename}".timer
systemctl disable "${basename}".timer
systemctl disable "${basename}".service
# Or , when running as user:
#enable, start
systemctl --user enable "${basename}".timer
systemctl --user enable "${basename}".service
systemctl --user start "${basename}".timer
#status
systemctl --user -n 50 status "${basename}".{timer,service}
journalctl --user -f -u "${basename}".service
systemctl --user list-timers --all
#stop, disable
systemctl --user stop "${basename}".timer
systemctl --user disable "${basename}".timer
systemctl --user disable "${basename}".service
# /etc/systemd/system/rsync_to_remotey.service
# or : /home/$USER/.config/systemd/user/rsync_to_remotey.service
[Unit]
Description=rsync USER X data to REMOTE Y
[Service]
Type=simple
ExecStart=/home/USERX/rsync_userx_to_remotey.sh
# /etc/systemd/system/rsync_to_remotey.timer
# or : /home/$USER/.config/systemd/user/rsync_to_remotey.timer
[Unit]
Description=Runs every 30 minutes rsync USER X data to REMOTE Y
[Timer]
OnBootSec=30min
AccuracySec=1h
OnCalendar=*:0/30
Unit=rsync_to_remotey.service
[Install]
WantedBy=multi-user.target
#!/bin/bash
# /home/USERX/rsync_userx_to_remotey.sh
set -x
pidfile=/tmp/"$(basename $0)".pid
if [ -r "$pidfile" ]; then
#pkill -P "$(cat ${pidfile})"
kill -TERM -"$(cat ${pidfile})"
sleep 4
fi
# trap based on http://linuxcommand.org/wss0160.php
trap "rm $pidfile; exit" SIGHUP SIGINT SIGTERM EXIT
# to kill children via pkill -P "$(cat ${pidfile})"
echo $$ > "$pidfile"
logsdir=/tmp #/var/log
logbasename="$logsdir"/"$(basename $0)"
USER=USERX
set -x
rsync --bwlimit=1024 -avAXHRP --delete -e 'ssh -i /home/'"${USER}"'/.ssh/id_ecdsa521_X' --exclude '/home/'"${USER}"'/.cache/*' /home/"${USER}"/ /mnt/foo/bar /mnt/zoo /mnt/baz "${USER}"@REMOTEY:/BACKUP/PATH/ 2> "$logsbasename".stderr | tee "$logsbasename".stdout
@shobaica
Copy link

shobaica commented Aug 6, 2020

How is the pidfile referenced by systemd script? Dooes it need to be referenced as follows:
PIDFile=/tmp/name.pid
Thanks,
Sal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment