SYSTEMD Timer Tutorial: http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html
I. Scheduled Tasks
Systemd timed tasks: Send an email every hour
second, the mail script
mail.sh Content
#!/usr/bin/env Bashecho "This is the Mail test" | /usr/bin/mail-s "Hi Tinywan" [email protected]
Execute script
1. Tips
Please install the "an MTA" on the "this" system if you want to use sendmail!
2. Installation SSMTP Service
sudo apt-get install SSMTP
3. Replace Sendmail/postfix with SSMTP
In general, many Linux distributions use Sendmail or Postfix as the default e-mail system, we use SSMTP to use external SMTP relay, so to configure the SSMTP as the system default, you first have to deactivate Sendmail or P Ostfix Services.
Systemctl Stop Sendmailsystemctl stop postfix
If you are using an earlier version of Linux, you can deactivate the service by using the following command:
Service SendMail stopservice postfix stop
Note: You also need to stop the SendMail or postfix service to boot with the system Oh, it is best to uninstall it directly.
4, configure SSMTP use QQ email address
Use Vim or any editor you like to edit the /etc/ssmtp/ssmtp.conf file, we use the SMTP server of QQ as an example, if you are using other SMTP, please make your own changes according to the official documents of the service provider:
[email protected]mailhub=smtp.qq.com:587[email protected]authpass=xxxxxxxxxxxxxxxxx Usetls=yesusestarttls=yesfromlineoverride=yesrewritedomain:qq.com
Note: The above red for you to use the QQ mail server request mail information, please configure the message information for you
After the configuration file is modified and saved, we can use the reliable mail service provided by QQ, and now first send a test email with the following command to see:
echo "This is a test e-mail" | Mail-s "Test Email" [Email protected]
When you send a test message, you encounter a prompt similar to the following:
Sendmail:cannot Open mail:25
That's because SSMTP will try to connect to the mail server using the standard SMTP 25 port by default, and if you're using an SMTP relay like Gmail or QQ with a non-standard TLS-encrypted port, you'll need to /ETC/SSMTP/REVALIASES the sender, server address, and port are indicated in the configuration file:
Www:[email Protected]:smtp.qq.com:587mainuser:[email protected]:smtp.qq.com:587
Continue testing after configuration
echo "This is a test e-mail" | Mail-s "Test Email" [Email protected]
MailTips Information
At this point, the mail delivery configuration is complete
Third,Service Unit
New Service is very simple, is to /usr/lib/systemd/system
create a new file in the directory, such as: tinywan-timer.service
files, you can write the following content.
Small knowledge:
Error encountered while saving file using vi new file under Linux:e212:can ' t open file for writing
The above problem is caused by the permission problem, the solution has the following ideas:
- 1. Log in with root and then operate.
- 2, in the use of commands, the front plus sudo.
- 3, if it is a multi-level folder file, because this folder is not created, so you have to create this folder, and then to manipulate the file.
Reference
1. ssmtp-let Linux system send mail from Office 365
2.
Linux command Details (12) Systemd Getting Started Tutorial: Using Timers to send mail (mail)