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.
1. I receive the email, but it only contains any errors that the script might output. (stderr)
2. A log file is created, containing the output of the script, minus the errors that I received in the email (stdout)
3. The name of the log file is not the date/time. It's the literal value of the variable (date -d '+1 hour' +'\%d_\%m_\%Y_\%H')
I am using Debian 11.5 and I was under the impression that vixie-cron was able to handle variables.
First, you aren't executing date. You need backticks to execute and set the DATEVAR. I think if you substitute backtick instead of double quote it will work.
Most people don't want to get those annoying emails on success, but you can figure out what you want later.
Lastlty, you redirect stdout to the file, so you don't get output via the cron mail. Use tee if you want output both to the file
and in the email. Tee replicates stdout to both places.
I’m going to guess that the path to date and/or tee is not known to the cron environment. Use absolute paths to all commands used. Use which to discover what the path is…e.g.:
Code:
which date
I’m also guessing that, since your command didn’t run, you’re looking at a old instance of the file. Use
Code:
ls -l
to see the date time on the file, and/or remove or rename the erroneous file(s) created before your test.
Also, you don’t need to wait an hour between tests. Just set the second column to (say) 5 minutes from “now” Change it back when everything else is working as you want.
In either case if you want an email when there is an error then remove the '2>&1' from that line. The output would not normally be emailed to you unless you added something like '&& echo "/root/reminders/logs/$DATEVAR completed"' to the end of that command line so it sends you a simple completion message each time it successfully runs.
If the crontab line read something like this you would get one message with an error and a different message when successful
The "sixth" field (the rest of the line) specifies the command to
be run. The entire command portion of the line, up to a newline
or a "%" character, will be executed by /bin/sh or by the shell
specified in the SHELL variable of the cronfile. A "%" character
in the command, unless escaped with a backslash (\), will be
changed into newline characters, and all data after the first %
will be sent to the command as standard input.
Write a script that creates the disk file (with whatever date-related filename you want to use) containing the data you want to email, then email the file from within the script. There's a copy of the output on disk and another sent to the user---which seems to be what you want. Then, have cron run that script.
cron is meant to execute commands/scripts. It's not really intended to be a programming environment.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.