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 03-03-2016, 12:48 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Crontab not writing a command to a log file


I have the following cron job who goal is to show disk free on a server at 12:01 am each day. Also want to write a time/date stamp to the log, because we are wanting to watch disk size grow. However it isn't writing anything to the following df.log file at all.


Code:
1 0 * * * /bin/df -ha > /root/output/df/`date +\%Y\%m\%d\%H\%M%\S\ -df.log 2>&1
I've even modified the crontab so it will run every 5 minutes, however nothing.

And to troubelshoot, I've removed 2>&1, yet still nothing getting written to that log.

Any recommendations on how to fix this?

thanks
 
Old 03-03-2016, 01:13 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
You're missing the end backquote, but even if you put it in there before "-df" it is escaped by the preceding backslash. You may also need the full path to "date". This works for me:

Code:
/bin/df -ha > /root/output/df/`/bin/date "+\%Y\%m\%d\%H\%M\%S"`-df.log 2>&1
 
1 members found this post helpful.
Old 03-03-2016, 01:53 PM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
I made the changes you recommended, set the crontab to run every 1 minute to test and still not working.

I'm looking thru /var/log/cron and don't see anything that stands out...
 
Old 03-03-2016, 02:04 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by JockVSJock View Post
I made the changes you recommended, set the crontab to run every 1 minute to test and still not working.

I'm looking thru /var/log/cron and don't see anything that stands out...
Just for info, as a one-off command, thesnow's suggestion works for me. Have you tried it as a one-off command to take cron out of the equation?
 
Old 03-03-2016, 02:16 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How about removing all of the date stuff, does that run?
Code:
1 0 * * * /bin/df -ha > /root/output/df/test.log 2>&1
 
Old 03-03-2016, 02:48 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by thesnow View Post
Code:
/bin/df -ha > /root/output/df/`/bin/date "+\%Y\%m\%d\%H\%M\%S"`-df.log 2>&1
or
Code:
/bin/df -ha > /root/output/df/$(/bin/date "+\%Y\%m\%d\%H\%M\%S")-df.log 2>&1
but you really, really, really want
Code:
/root/output/df/\2016\03\03\15\44\24-df.log
as the output?

Code:
echo /root/output/df/$(/bin/$(date "+%Y-%m-%d-%H-%M-%S")-df.log
is readable, and cleaner?
If you agree, try
Code:
/bin/df -ha > /root/output/df/$(date "+%Y-%m-%d-%H-%M-%S")-df.log 2>&1
which gives
Code:
/root/output/df/2016-03-03-15-45-40-df.log

Last edited by Habitual; 03-03-2016 at 02:54 PM.
 
Old 03-03-2016, 04:04 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Habitual View Post
but you really, really, really want
Code:
/root/output/df/\2016\03\03\15\44\24-df.log
as the output?

Code:
echo /root/output/df/$(/bin/$(date "+%Y-%m-%d-%H-%M-%S")-df.log
is readable, and cleaner?
Cron uses the delimiters to pass a literal "%" to date. You only see them when you run date on the command line, when you run it through cron they disappear. Without them you'll get errors out the wazoo from cron when it tries to run the command.
 
Old 03-04-2016, 08:38 AM   #8
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by suicidaleggroll View Post
How about removing all of the date stuff, does that run?
Code:
1 0 * * * /bin/df -ha > /root/output/df/test.log 2>&1
Yes it did run at 00:01, the time I want, so that is good.

However I did what Habitual recommended and rolled the time back on the crontab to every minute and still didn't write to that log.

However I ran the following from the CLI:

Code:
/bin/df -ha > /root/output/df/$(date "+%Y-%m-%d-%H-%M-%S")-df.log 2>&1

The weird thing is that it didn't write to df.log however it created the following file: 2016-03-04-08-33-33-df.log. I'm not sure why it didn't write to the df.log file Vs creating a new file.
 
Old 03-04-2016, 09:39 AM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by JockVSJock View Post
Yes it did run at 00:01, the time I want, so that is good.
That's good

Quote:
Originally Posted by JockVSJock View Post
However I did what Habitual recommended and rolled the time back on the crontab to every minute and still didn't write to that log.
What makes you think that?

Quote:
Originally Posted by JockVSJock View Post
However I ran the following from the CLI:

Code:
/bin/df -ha > /root/output/df/$(date "+%Y-%m-%d-%H-%M-%S")-df.log 2>&1
it created the following file: 2016-03-04-08-33-33-df.log.
Good

Quote:
Originally Posted by JockVSJock View Post
I'm not sure why it didn't write to the df.log file Vs creating a new file.
Because that's what you told it to do. What did you think "command > /root/output/df/$(date "+%Y-%m-%d-%H-%M-%S")-df.log" was supposed to do if not write the output of the command to a dynamic filename using the current system date/time???
 
  


Reply

Tags
cron, crontab, date, df



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
Which Process is writing to log file ZAMO Linux - General 4 08-17-2016 09:54 PM
[SOLVED] script in called by crontab executing but not writing data to file. Dafydd Programming 2 08-02-2012 01:35 AM
Crontab does not create log file montblanc Linux - Newbie 4 02-05-2008 08:59 AM
crontab <file> command Derrick Thatcher Linux - Software 2 03-08-2006 04:29 AM
Writing a crontab file Gnustome Linux - Software 2 11-02-2003 04:27 AM

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

All times are GMT -5. The time now is 04:07 PM.

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