logrotate dateformat does not support milliseconds extension - logrotate

I am using logrotate. It works fine with dateformat %s and generates files in somefile.log.1555267419.gz. But I need to add the extension in milliseconds dateformat(somefile.log.1555267419789.gz).
I checked the manpage and as far as I have understood, It says it does not support milliseconds specifier.
Is there any way to add milliseconds in extension and still be able to rotate the old log files?
/var/log/somelog/*.log {
compress
notifempty
daily
copytruncate
size 15M
dateext
dateformat .%s
rotate 20
}

As you have already mentioned, the logrotate docs mention no support of milliseconds. You can get the EPOCH time using %s, assuming the system clock is past Sept 9 2001.
dateformat format_string
Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.
However, you could try to set EPOCH with millisecond granularity using logrotate scripts manually. Here is an example where you call a custom script located at /my/script which takes the rotated filename as the parameter. Using $1 in the prerotate/postrotate you get the absolute path of the file being rotated. You also want to use the nosharedscripts config (which is set by default) to make sure the prerotate/postrotate scripts are run per log file matching your *.log pattern.
/var/log/somelog/*.log {
compress
notifempty
daily
copytruncate
size 15M
dateext
dateformat .%s
rotate 20
nosharedscripts
postrotate
/my/bash/script $1 > /dev/null
endscript
}
In your custom script, you can then set milliseconds on the file name by renaming it. One way to get the EPOCH with milliseconds on a Linux machine is to run date "+%s%3N".

Related

Bulk change date to present date with ExifTool

I have a bunch of images with different create dates. I want to normalize them all to a given date (say today's date) using a batch file (Windows). Can ExifTool set dates? I only see documentation and examples for shifting dates. To shift the date to present, I would need to somehow read the date for each file, calculate the difference, and then shift. That would be ok, but I don't know how to read the create date into a variable using ExifTool.
One obstacle for me is that I don't speak Perl. I do Python, and there is pyexiv2. This allows to write the "date", but I can't see anywhere if that is just create date or all dates.
Edit
Here shows using, for example,
exiftool -AllDates='2010:08:08 15:35:33' -overwrite_original IMG_01.jpg
in Ubuntu linux, but I could not get that to work in Windows.
As a hack, I tried
exiftool -AllDates+=2015:03:02 IMG_8220.JPG
which set the dates to the time executed, probably because the shift was so completely crazy. But I'd like to have control, and, specifically be able to change YYYY:MM:DD without changing the time.
Consequently, help still appreciated.
I crossposted to the ExifTool forum, and ExifTool author Phil Harvey responded that you need to use double quotes. The single quotes from the linked blog post don't do it in Windows.
So, one would use
exiftool -AllDates="2010:08:08 15:35:33" IMG_01.jpg
I tested it and (of course) it worked.

How to get the modification date of a file in Racket?

I need to determine what has changed in the file system.
How can I a get the time when a file was last modified?
The modification time of a file will always be returned by file-or-directory-modify-seconds. This will also return the modification date of directories except from the FAT format file systems - for directories a FAT file system will return the creation date instead.
To convert the file modification time, or any time in seconds to a more readable format you can use seconds->date.
> (require racket/date) ;for date->string only
> (date->string
(seconds->date
(file-or-directory-modify-seconds
(string->path "/home/ben/.emacs"))))
"Wednesday, June 4th, 2014"
reference: racket discussion list

Logrotate doesn't work automatically (using size limit)

It seems that my rotation is not working. If I execute the logrotate manually it's working like it should. The logrotate is ran because I can see it in the logs. Here's what I have:
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
/home/www/logs/access_log {
sharedscripts
delaycompress
size 2G
compress
dateext
maxage 30
postrotate
/usr/local/bin/apachectl -k graceful
endscript
}
Any clue?
It's an old question, but someone might find this helpful.
The default rotation settings indicate weekly rotation, but your configuration for access log below specifies rotation by size.
These settings will not work at the same time. It's either time or size.
For access logs, I would advise daily rotation.
If your logs grow over 2GB during the day, then you will need to run logrotate hourly. This ensures that logrotate will check the size of your log and rotate accordingly.
This however, implies that you have to add time stamp to your logs, because you would want to have multiple logs for the same day, right?
There is also maxsize parameter for logrotate, and it's supposed to work together with time based rotation (daily, weekly etc) but I'm not sure if it works. You will need to experiment.

C++ Getting local time for saved games

I am making a game off Ubuntu through the terminal and one feature I would like to add is adding timestamps to the saved games.
Like the game would display "Save: (time)" and in time would be (DD/MM/YYYY HH:MM) is there a simple way to do this?
In order to get the current date you can use the command date. See man date for more format options.
$ date +'%d/%m/%y %T'
26/10/13 16:29:56
In case you would like to compute the timestamp from C++, see how to get local time and put it in a string.

Batch file: Get yesterday's date in format: M_d_yyyy

How does one get yesterday's date format in a batch file?
I'd like it to look like so: M_d_yyyy
Note that if there's a single digit day and month, I'd like it to be single digits.
Example: 8_5_2013 is August 5th, 2013.
I looked around for a few days but couldn't find a solution.. any lead is much appreciated.
Nothing wrong with free 3rd party executables, but some of us are not allowed to use them on our work machines.
I have written a powerful hybrid JScript/batch utility called getTimestamp.bat that can do nearly any date and time computation on a Windows machine.
There are a great many options for specifying the base date and time, many options for adding positive or negative offsets to the date and time, many options for formatting the result, and an option to capture the result in a variable. Both input and output can be directly expressed as local time, UTC, or any time zone of your choosing. Full documentation is embedded within the script.
The utility is pure script that will run on any modern Windows machine from XP forward - no 3rd party executable required.
Assuming getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following simple call will define a dt variable containing yesterday's date in M_D_YYYY format:
call getTimestamp -od -1 -f {m}_{d}_{yyyy} -r dt
Note: when I put a date in a file name, I like to use YYYY_MM_DD format because that format will sort chronologically when getting a directory listing.
I think you should get date.exe from UnxUtils.
date.exe --date="1 day ago" "+%-m_%d_%Y"
Download: http://sourceforge.net/projects/unxutils/files/unxutils/current/
Man page: http://www.ss64.com/bash/date.html
#echo off
setlocal
set magic="c:\unx\usr\local\wbin\date.exe" --date="1 day ago" "+%%-m_%%d_%%Y"
for /f %%i in ('%magic%') do set yesterdate=%%i
echo yesterdate = %yesterdate%
If you want to do it with just batch language, you'll end up with nearly 100 lines of incomprehensible batch code. UPDATE: or use dbenham's hybrid batch/JScript solution posted in the answer below, which at least uses sane Windows APIs.
See Also:
How to get current datetime on Windows command line, in a suitable format for using in a filename?