Skip to content

Instantly share code, notes, and snippets.

@hrchu
Created March 20, 2020 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrchu/30a81b5a81a36af36678cc46d2d06bdf to your computer and use it in GitHub Desktop.
Save hrchu/30a81b5a81a36af36678cc46d2d06bdf to your computer and use it in GitHub Desktop.
Daily report from systemd service/timer via email

TRY IT

  1. sudo apt-get install heirloom-mail
  2. config your email address and MTA server in send-mail.sh
  3. sudo cp send-mail.sh /opt/send-mail.sh
  4. sudo cp dummy.* /etc/systemd/system/
  5. execute sudo systemctl start dummy.service to confirm it works.
  6. sudo systemctl start dummy.timer
  7. Check the schedule via systemctl list-timers
[Unit]
Description=A dummy service
[Service]
ExecStart=/bin/date # Replace this with your application
ExecStopPost=/opt/send-report.sh
[Unit]
Description=A dummy timer
[Timer]
OnCalendar=Daily
[Install]
WantedBy=multi-user.target
#!/bin/bash
mailx -v -s "Systemd service/timer report" -S smtp=smtp://[SMTP Server] -S from="[SENDER EMAIL]" [RECEIVER EMAIL] << EOF
$(journalctl --since "today" -u dummy)
EOF
echo 'Report sent. Bye for now.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment