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.
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 ?
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.
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:
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.
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
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
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.