LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-29-2022, 11:20 PM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Rep: Reputation: 3
variables in a cron job not working properly


I have a cronjob running a script every hour. I want it to email me the output, and also write the output to a log file. Both stdout & stderr.

Code:
MAILTO="me@myemail.com"
DATEVAR="date -d '+1 hour' +'\%d_\%m_\%Y_\%H'"
0 * * * * root /root/reminders/tasks/hourly >> /root/reminders/logs/$DATEVAR
There's a few issues with this though.

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.

What I'm I doing wrong here?
 
Old 10-29-2022, 11:52 PM   #2
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 415
Blog Entries: 2

Rep: Reputation: Disabled
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.

Second, to map stderr to stdout, put 2>&1
http://www.linuxask.com/questions/me...-21-in-crontab

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.
 
Old 10-30-2022, 12:08 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
I think this is everything you mentioned

Code:
MAILTO=me@myemail.com
DATEVAR=`date -d '+1 hour' +'\%d_\%m_\%Y_\%H'`
0 * * * * root /root/reminders/tasks/hourly 2>&1 | tee /root/reminders/logs/$DATEVAR
I'll know in a hour when the job runs again.
 
Old 10-30-2022, 02:15 AM   #4
aristosv
Member
 
Registered: Dec 2014
Posts: 263

Original Poster
Rep: Reputation: 3
I got an email saying

tee: invalid option -- 'd'
Try 'tee --help' for more information.

and the log file still kept the name "date -d '+1 hour' +'\%d_\%m_\%Y_\%H'"
 
Old 10-30-2022, 03:09 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,748

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
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.

Last edited by scasey; 10-30-2022 at 03:10 AM.
 
Old 10-30-2022, 07:55 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Since a cron job is not associated to a terminal window the tee command will not work as expected.

Are you using root's crontab (i.e crontab -e ) or a system cron job i.e /etc/crontab? If you are using root's crontab then a username is not required.

Code:
0 * * * * /root/reminders/tasks/hourly 2>&1 | tee /root/reminders/logs/$(date -d "+1 hour" +'\%d_\%m_\%Y_\%H')
 
Old 10-30-2022, 08:07 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Use $( ) instead of ` `
cron requires a % backslash-escaped, so ensure there is no further quoting (or the shell will do literal backslashes)
Code:
MAILTO=me@myemail.com
DATEVAR=$( date -d '+1 hour' +\%d_\%m_\%Y_\%H )
0 * * * * root /root/reminders/tasks/hourly >> /root/reminders/logs/$DATEVAR
 
Old 10-30-2022, 08:28 PM   #8
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
With my version of the date command the \ is an issue.
with the backslash I get
Code:
$ date -d '+1 hour' +'\%d_\%m_\%Y_\%H'
\30_\10_\2022_\21
and without it I get
Code:
$ date -d '+1 hour' +'%d_%m_%Y_%H'
30_10_2022_21
So I would suggest you try
Code:
MAILTO=me@myemail.com

0 * * * * root DATEVAR=$( date -d '+1 hour' +%d_%m_%Y_%H ) ; /root/reminders/tasks/hourly 2>&1 >> /root/reminders/logs/$DATEVAR
As noted your script may be correct if it is in /etc/crontab, but if it is in root's personal crontab then it should read as
Code:
MAILTO=me@myemail.com

0 * * * * DATEVAR=$( date -d '+1 hour' +%d_%m_%Y_%H ) ; /root/reminders/tasks/hourly 2>&1 >> /root/reminders/logs/$DATEVAR
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
Code:
0 * * * * DATEVAR=$( date -d '+1 hour' +%d_%m_%Y_%H ) ; /root/reminders/tasks/hourly > /root/reminders/logs/$DATEVAR && echo "/root/reminders/logs/$DATEVAR completed"
 
Old 10-30-2022, 09:18 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
From the crontab man pages
Quote:
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.
 
Old 10-30-2022, 09:33 PM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by aristosv View Post
I have a cronjob running a script every hour. I want it to email me the output, and also write the output to a log file. Both stdout & stderr.

Code:
MAILTO="me@myemail.com"
DATEVAR="date -d '+1 hour' +'\%d_\%m_\%Y_\%H'"
0 * * * * root /root/reminders/tasks/hourly >> /root/reminders/logs/$DATEVAR
Simplify, simplify.

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.

HTH...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Job Not Running - Looks Like Cron Tried Noble User Linux - Newbie 7 10-26-2014 10:26 AM
how to abort cron if the previous cron job not yet finished? Winanjaya Linux - Newbie 2 05-22-2012 06:44 PM
Cron job is not recognizing environment variables Jon Blanchard Linux - Newbie 6 02-17-2012 09:50 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration