LinuxQuestions.org
Review your favorite Linux distribution.
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-26-2012, 05:34 AM   #1
yashvanth
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Rep: Reputation: Disabled
Post help in shell/bash script


hi all
i am running the script and a cron to take the backup every day at some time.
i like to create logfile automatically all the time when cron runs and logfile name should contain the date so we can easily locate it.
and also like to write into output of terminal (about backup process) into newly created log file



1.how to crete the logfile by that name automatically?
2.how to write into the newly cretaed logfile ?

Last edited by yashvanth; 03-26-2012 at 06:53 AM.
 
Old 03-26-2012, 05:53 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sounds great ... didn't seem to be any question though?
 
Old 03-26-2012, 06:38 AM   #3
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Do you really want to ask something? If not, then it better be moved to general.

BTW grail, just 7 posts away from being a Guru.
 
Old 03-26-2012, 06:41 AM   #4
yashvanth
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
re

hi chaitanya
are you replying to my original post or to the reply of other person.

i hope i put the question in good way only
 
Old 03-26-2012, 06:44 AM   #5
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Obviously your original post.
 
Old 03-26-2012, 06:46 AM   #6
yashvanth
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
re

can you tell what is wrong in that post
i presented in good way only
i need to achieve those things i mentioned in the oroginal post

help me if you know how to do that one
thank you
 
Old 03-26-2012, 06:49 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by yashvanth View Post
can you tell what is wrong in that post
i presented in good way only
i need to achieve those things i mentioned in the oroginal post

help me if you know how to do that one
thank you
You posted what you want to achieve, but not what you have done already and what your problems are.
 
Old 03-26-2012, 07:08 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
yashvanth, what have you tried so far? Did you stick at some point? The following steps are required:
  1. Decide a backup strategy and choose the more suitable tools to accomplish this task (for example rsync)
  2. Write a script and test it on the command line
  3. Set up a cron job and test it a couple of minute from the current time until you reach the desired result.
Please, provide as many details as possible so that it's easier for other LQ members to help you.
 
Old 03-26-2012, 07:18 AM   #9
yashvanth
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
Smile re

hi i created a cron job (which will run dialy) and a shell script for rsync and it is working fine.
now i am trying to create one logfile with name as logfile_currentdate automatically whenever cron runs daily and the output of cron like process details should go into that logfile.
 
Old 03-26-2012, 07:31 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nice. In your crontab entry you can add redirection of both standard output and standard error to a file, using command substitution to generate the date, example:
Code:
0 4 * * * /path/to/script.sh > /path/to/logfile_$(date +\%Y\%m\%d) 2>&1
Please, notice the escaped % signs in the date format. They are mandatory in crontab, since the % sign has a special meaning here. From man 5 crontab:
Quote:
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
 
Old 03-26-2012, 07:34 AM   #11
yashvanth
LQ Newbie
 
Registered: Mar 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
Smile re

in the cron entry you wrote path/to/logfile
is that mean there should be an already existing logfile?
i need to create new logfilewith date in the logfile name whenever cron runs everyday.is there any way to do this work ?
thank you
 
Old 03-26-2012, 07:42 AM   #12
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
No its not mandatory to have the file. If you use append redirection ">>", it will create the file inside the folder you specify.
 
Old 03-26-2012, 07:58 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by yashvanth View Post
in the cron entry you wrote path/to/logfile
is that mean there should be an already existing logfile?
thank you
As already noticed, the logfile might not exist and in that case the file will be created. Instead, it is a good rule to use absolute path names in crontab entries, otherwise they will be relative to the user's home directory. Another good rule is to use absolute paths for commands (either in the crontab entry or inside the script) since crontab has a very limited environment and especially the PATH environment variable is limited to /bin:/usr/bin.
Quote:
Originally Posted by yashvanth View Post
i need to create new logfilewith date in the logfile name whenever cron runs everyday.is there any way to do this work ?
The code I suggested does exactly this. It creates a new logfile every day. If you run the cron job more than once a day, you have to refine the date format including (at least) the hour, but - as written - a daily cron job will create a different log file.

@Chaitanya: what do you mean by ">>", it will create the file. The file will be created even by single redirection ">" or am I missing something?
 
Old 03-26-2012, 08:10 AM   #14
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
I meant to say the file will be created by the redirection. You had already mentioned the use of ">". Append redirector ">>" just an addition to what you said.
 
  


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
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
help with bash shell script !! taiwf Linux - Newbie 5 06-11-2006 06:07 PM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash shell script naka0naka Linux - Newbie 7 05-28-2004 03:06 PM

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

All times are GMT -5. The time now is 04:11 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