Score:-2

When creating a file naming template for core dump can you modify the time specifier from unix epoch time to human readable form on the fly

cn flag

I read the man core and man date pages and four quality webpages on the topic core dumps. I've been studying and practicing making core dumps for four days now.

The question is well stated in the title of this post.

Note: when I refer to 'core' I also am referring to the kernel parameter kernel.core_pattern and will call it an environment variable. I call it an environment variable because it acts like an environment variable on the global scope. I use these terms interchangably.

core describes in the man page how to set this environment varaible to match a template. The template offers a 'time specifier' %t that provides to the template the current time in unix format, seconds since epoch.

As per core guidelines this script next is an applicable template:
core.%e.%t

%e is executable filename
%t is time of dump, expressed as seconds since the Epoch
core is the file's name. It could be core or any name you give it.

Note: there could be a path that leads to where you want the file placed; but, I will write to current working directory; and, I will make reading the script here simple, so I omit the path.

We can set the environment variable like this:
sudo sysctl -w kernel.core_pattern=core.%e.%t

Now, I require the date in two-digit year two-digit month two-digit day format, like 220224. The problem is the time specifier %t of core provides time in seconds since the epoch, not very human friendly!

I also required that the date become the most current date upon execution of the script. The time specifier %t of core will do this; but, the result is the file's name reads with a unix time stamp, not very human friendly!

Conversely, if I substitute a script in the location of core's time specifier while setting the environment variable kernel.core_pattern then core's naming template will always be written with the date inscribed at the time the template was created and not the actual time of the core-dump.

The script I have contrived to do this is next. I just replaced the core time specifier in the template with the snippet provided here: date -d "-8 hours" +%y%m%d

But remember if I use a script in place of core's time specifier the time set in the template becomes hard coded and will not be accurate the next time I have to pull a core-dump.

My scripts looks like this:
sudo sysctl -w kernel.core_pattern=core.%e.`date -d "-8 hours" +%y%m%d`; ~/test.out

The ~/test.out portion of that script runs a test file I am using to learn how to troubleshoot linux installations.

My script sets the environment variable right before calling the testing script. The benefit of this is the core-dump file gets named just as the testing script is called; and, this gives the core-dump file a meaningful name. My script works; yet, every time I need to pull a core-dump I have to remember to accompany the call to the testing script with my script. If I do not combine the assignment to the environment varaible with the call to the testing script the date written to the core-dump file becomes whatever it was set to the last time the assignment to the environment varaible was set, not very accurate.

I want to rewrite my script to get the best of having the date written in human readable format as well as the date written at the time the core dump occurred. I have tried!

One problem I encountered arrises because the date command uses the same format control token %t as does the core template specifier token %t

This is a test to confirm the conflict of the core time specifier and date command's format control:

  this token is meant to be from the core time specifier 
    sudo sysctl -w kernel.core_pattern=core.%e.`date -d@%t +%y%m%d`; ~/test.out

This gives a "date: invalid date ‘@%t’" message

Do you see we can not just drop in place the core time specifier in the time location of the date script.

This is a test to confirm the script works when used as intended:

                             This is a Unix epoch timestamp 
    sudo sysctl -w kernel.core_pattern=core.%e.`date -d@1645337785 +%y%m%d`; ~/test.out

This works as expected except the date remains constant.

Do you see the unix time stamp can actually drop in place of the time location of the date script.

There maybe another way to do this; but, I do not know. I am asking if any of you see a way around this.

Thanks for your time in advance.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.