systemdを使用して定期的に電子メールを送信する

systemdを使用して定期的に電子メールを送信する

使いたいシステムタイマー記念日や税務申告書など、特定の事項を思い出させるために定期的に電子メールを送信してください。

メールを定期的に送信します。愚か;追加ソフトウェア(例:メールを送信

私はArch Linux 4.18.5を使用しています。systemctl --versionsystemd 239は言います。

ベストアンサー1

~/.config/systemd/user/send-mail.serviceまず、次の内容でsystemdサービスファイルを作成します。

[Unit]
Description=Sends mail that reminds me of an anniversary

[Service]
; The l flag for bash creates a login shell so Mutt can access our environment variables which contain configuration
ExecStart=/bin/bash -lc "echo \"$(whoami) created this message on $(date) to remind you about...\" | mutt -s \"Don't forget...\" [email protected]"

以下を実行して、電子メール送信が機能しているかどうかをテストできます。

systemctl --user daemon-reload && systemctl --user start send-mail.service

に電子メールを送信する必要があります[email protected]

~/.config/systemd/user/send-mail.timer次に、次の内容でタイマーを作成します。

[Unit]
Description=Timer for writing mail to myself to remind me of anniversaries

[Timer]
; Trigger the service yearly on September 5th
OnCalendar=*-09-05
; Send a mail immediately when the date has passed while the machine was shut down
Persistent=true
AccuracySec=1us
; Set the timer to every ten seconds (for testing)
; OnCalendar=*:*:0/10

[Install]
WantedBy=timers.target

タイマーの内容はサービスを参照しません。サービスとタイマーの.service名前はサフィックス sum を除いて同じであるため、まだ機能します.timer。タイマーとサービスの名前を異なるように指定するには、Unit=タイマー[Timer]セクションでその名前を使用してください。

起動時にタイマーが起動するように設定

systemctl --user daemon-reload && systemctl --user enable send-mail.timer

これでタイマーを見ることができますsystemctl --user list-timers --all

タイマーを開始するには:

systemctl --user start send-mail.timer

systemdが日付をどのように解釈するかを確認するには、systemd-analyze calendar *:0/2またはを使用できますsystemd-analyze calendar quarterly。また確認してみてください。手動systemdの時間形式情報。

おすすめ記事