Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New log file every time cron job runs

I have figured out how to output cron jobs to a log file using >> and providing a path to the log file. The >> appends log information to the existing file. How can I make it so the cron job creates a new log file every time it runs? (i.e. rsync.log, rsync(1).log, rsync(2).log -- ideally though I'd like the file name of the log to be something like DD-MM-YY.log).

I want a separate log file so this one log file doesn't get so huge and if/when we go to look if a file/folder was successfully backed up (I'm running an rsync command in the cron job) we don't have to comb through a MASSIVE log file.

Additionally, when the cron job is outputted to the log file, there are no time/date references. Example of my first log output:

**sending incremental file list**
**sent 78 bytes  received 11 bytes  35.60 bytes/sec**
**total size is 0  speedup is 0.00**

That's it. The timestamp on this logfile will keep changing every time the job is run, so we wouldn't even be able to tell what day that particular file/folder was copied via rsync. If I had a separate log file for every time the cron job ran, I could just open the log file for the particular date that it was created and view what was backed up.

My current cronjob:

*/1 * * * * rsync -avz /home/me/test/ [email protected]:test/ >>/home/me/cron_logs/homedir_backups/rsync.log 2>&1

I only have it set to 1 minute for testing purposes. Eventually this will only run daily at midnight.

like image 882
00fruX Avatar asked Oct 28 '14 22:10

00fruX


People also ask

How do I run a cron job on Linux?

You can run cron jobs as any user. Every user on Linux can use a crontab file to run their own set of cron jobs. By default, a user doesn’t have a crontab file on Linux. You can create a crontab file with the following command: If you’re running this command for the first time, then you should be asked to pick a text editor from the list.

How do I save Cron output and errors to a log file?

When the cron job runs, it outputs the script’s result to the standard output (STDOUT) or standard error (STDERR). If you want to save both the output and errors to a log file, this tutorial tells how to do it. Open the crontab with your preferred text editor: Add output redirection to the end of the line:

How often should I run a cron job?

Running cron job every 5, 10, or 15 minutes are some of the most commonly used cron schedules. Crontab (cron table) is a text file that defines the schedule of cron jobs. Crontab files can be created, viewed , modified, and removed with the crontab command.

What does error 2>&1 do in cron job log?

2>&1 tells the cron to send errors (STDERR) to same target than normal output (STDOUT) Now, after each cron job execution, you will see output and error in the log file. If you don’t see the file at all, you might try to create an empty file first.


1 Answers

If you want time stamped logs, you add the string of a formatted date command, date +%d%y%m for day month year in number format.

You can use backtick to put the string in the cron:

~$ /home/me/cron_log-`/bin/date +%d-%m-%y`

So the file name will have the current date appended to it. The backtick says "run this command and put the output here as a string".

Now the problem is that your directory might get huge and then you have to write a short script to delete them by time. I have a script that reads the format and keeps X and deletes the rest but most people would just use "find" to delete by time older stuff, like logs that have mtime > 1 year.

like image 142
Eugene Tsuno - NOAA Affiliate Avatar answered Sep 22 '22 02:09

Eugene Tsuno - NOAA Affiliate