[SOLVED] Automatically send email at the end of month but on computer that is not on all the time
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Automatically send email at the end of month but on computer that is not on all the time
I have been reading on this and am not entirely sure what the best way to do this is. What I need to do is have a script (which does nothing more than collects some data and sends it in an email) run once a month. However, it cannot be run earlier than, say, the 25th day of the month. Also, this is on a laptop that is not on 24/7 nor necessarily used daily.
So, I see that cron would not be good for this, since if the computer is off when the event occurs it will not perform the task. Anacron, on the other hand, can act so that it performs the task when it first can at some specific interval. Anacron, however, apparently does not allow for more specific timing than "once a month", in other words I cannot specify that I want this to occur within the last 5 days or so of the month, and not in the beginning.
I read a proposal on the net that one can have a daily cron job query the day and then use a logical comparison such as "is date > 25" and if not then exit, otherwise perform task. However, to me this seems a bit odd, that is to perform a task 30 times a month for an email that needs to be sent once. Is there some better way?
Last edited by pylkko; 03-14-2016 at 11:57 AM.
Reason: typo
I don't think that's a bad way; however what if the computer is also not turned on between the 25th and the end of the month? Perhaps also record the last day a report was sent and if it has been longer than 25-30 days, consider making a new report for that condition too.
I don't think that's a bad way; however what if the computer is also not turned on between the 25th and the end of the month? Perhaps also record the last day a report was sent and if it has been longer than 25-30 days, consider making a new report for that condition too.
I am almost 100% sure that that will never happen, that is, that this particular computer will never be unused for so many days in a row. However, just to be safe the script could "have memory", in other words, maybe something like i = i + 1 after the performance. After that it would need to check on two conditions prior to performing the deed 1) is i > 0 and 2) is date > 25. Also, of course, would require zeroing i on the fist day of every new month some how.
Maybe it is not a problem to have a script run like this "all the time". It's not like it's going to eat a lot of cpu cycles or something.
This laptop also runs systemd, and thus maybe could do this same thing using a .timer
Maybe it is not a problem to have a script run like this "all the time". It's not like it's going to eat a lot of cpu cycles or something.
I agree and that's exactly what I was saying which is that I do not believe it is a huge deal to run a script every time you boot, if it has nothing much to do, then that's all of it, and the one time per month it needs to do it's work, it is primed to accomplish that task and then go into reduced mode for the next month's time.
Are you by chance saving log files of some type? I've never configured exactly stuff like this, but I believe you can use logrotate to do some of this stuff and in fact, remove the one month item and just go by log file size instead. You clearly can run a script post rotating of files to send a mail.
I now realize that systemd already has all these functions implemented in it. That is, it is possible to set a day of the month at which to perform task (and the computer can be off), it can send a mail in case of problem, and also the Persistent = true option can even store the last time the task was performed on disk and use that to decide what to do. And it is as easy as making a normal .service and then running that with a .timer.
The stuff that I am collecting are not logs, but other work that people have done, so logrotate is not going to work here.
Last edited by pylkko; 03-14-2016 at 10:55 PM.
Reason: typos
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.