How to save separate cron file in terminal without writing multiple lines - command-line

I set it to run every minute. I have this:
* * * * * curl link > filepath/filename.json
However, the file is overwriting every minute.
I would need a file for each run, so 1440 files a day.
(I'm on a Mac if that makes any difference.)

Related

Reading the last 2 lines of a .log file via Matlab

The problem that I am attempting to solve is as follows:
I have a .log file that is updated every x seconds (an interval that I can change), with updated status information from a piece of test equipment. At each interval, another line is added to the .log file, with the updated information. My goal is to have the most recent status information (the last two lines of the .log file) easily viewable in Matlab.
Here is an example of what each update looks like, in case that is relevant (a single line of text):
What I have tried:
I used the readtable command (shown below) to view the information in the .log file, but that gives me the entire .log file every time the function is called, when I only want/need the last two lines.
data = readtable('FileName.log','FileType','text')
I know that this would be simpler if I was working with a .csv or .xlsx file, but the test equipment only updates the .log file, so I cannot just change the file type, as it would not get updates then.
Any advise would be appreciated.
If the .log file is in plain text format (as I assume based on your code snippet), you can get the last 2 lines of the file by using the following system command in MATLAB:
[status,output] = system(['tail -n 2 ', path]);
Please do keep in mind that this requires the tail command to be available, which is not available in windows by default - however you can get around this by installing a package containing the tail command, for example Cygwin.

Running MATLAB system command in background with stdout

I'm using MATLAB and calling an .exe via the system command.
[status,cmdout] = system(command_s);
where command_s is a command string that is formatted earlier in my script to pass all the desired options to the .exe. The .exe would normally write to a .csv file via the > redirection operator in Windows/DOS. Instead, this output is going to cmdout where I use it later in the MATLAB script. It is working correctly and as expected. I'm doing it this way so that the process just uses memory and does not write a very large file to the disk, which would then have to be read from the disk and then deleted after I'm done with it. In the end, it saves a .mat file that's usually in hundreds of KB instead of 10s/100s of MBs as the .csv file would be (some unneeded data is thrown out in the end).
The issue I'm having is since I'm dealing with large files, the executable can take a significant amount of time. I typically have to wait about 2 minutes after executing this command. In the meantime, I have no feedback to know it is progressing and that my system hasn't froze. I know I could add the & symbol to the end of my string, command_s, and run MATLAB code while this is running in the background (or asynchronously as some would say), but that brings up an external window AND makes cmdout empty - so I cannot use the output - forcing me to sit there for 2 minutes wondering each time it executes.
Is there any way to run in the background AND get the stdout from the command?
Maybe you could try system(command_s,'-echo')?

How to get time of creation of a file? (MATLAB)

I want to read an image file in MATLAB and use the time it was created in the system to add a delay for my next command.
For ex if time_created is the system time the image file was created, I want my next command to execute after a delay of (time_now - time_created) + 3 sec. Is this possible?
You can use the information given by dir:
yourFileName = 'myFile.m'
allfiles = dir
filenames = {allfiles(:).name}
[~,idx] = ismember(yourFileName,filenames)
yourFileDate = allfiles(idx).date
which will return a date string:
yourFileDate = 06-Mar-2014 10:53:48
or alternatively:
yourFileDate = allfiles(idx).datenum
which will give you the output in datenum format. (You probably want to work with that)
you could then continue as follows:
timeNow = clock %//current system time as date vector
timeFileCreation = datevec(yourFileDate) %//file creation time as date vector
timeDiff = etime(timeNow,timeFileCreation)
returns the number of seconds between both time vectors.
Note that the matlab 'dir' command does not return the file creation time, but rather the last file modification time. In your use case, the creation time and the modification time are likely the same. But in other circumstances, the modification time may be different than the creation time (e.g. if a user edited a file after it was created). Different operating systems store different file times between the creation, last modification and last access times. Last modification time is the only one that is available in all operating systems that matlab runs on, so it is the only time supported by the matlab 'dir' command. Depending on with OS you're running on, you can access the other file times available on that OS, through (at least) use of the 'system' command and knowledge of the command line functions available in that OS.
With Python now really integrated into MATLAB, this works on both Windows and MacOS:
d1 =datetime(py.os.path.getctime('video_path'),'ConvertFrom','epochtime','TicksPerSecond',1,'Format','dd-MMM-yyyy HH:mm:ss.SSS');

Extract the most recent values from appended .CSV in MATLAB

I have a .csv file which is appended with 3 new values in the row below the previous set:
dlmwrite('MyFile.csv', [MyValue,MyValue2,MyValue3], '-append');
This happens every minute. It happens indefinitely because of a timer i.e it accumulates data over time:
How can I continually copy over the 60 most recent sets of values from the file and store them in a new csv file, say MyFile2.The row number of the .csv file is increasing by 1 with every minute. i.e 60 values stored in 60 minutes but I may have 100 values and want to extract the latest 60 for another file.
Image of the CSV file - 2nd column is time in HOURS:MINUTES without the : separator (ignore the lapse in time between rows 38 and 39 or any lapses elsewhere):
Note: MyValue is added to the file every minute because the script is run every 60 seconds from another script. I.e there is no internal timer in the main script:
Period = 60; % Update period in seconds
tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', 'TESTINGFINAL');
start(tim)
stop(tim)
runtmp = fullfile('MyScriptLocation','MyScript');
run(runtmp);
If you want to do this continuously while running I'd suggest some sort of circular buffer arrangement so you always have the last 60 values in memory. This will be easier than trying to work out the current length of your continuously logging file. Basic idea (minus the actual timing code):
% initialising buffer
MyValue1 = zeros(60,1);
while true % for certain values of true
% these go once a minute
mv1 = myfunc1(inputs);
MyValue1 = [MyValue1(2:end); mv1];
dlmwrite('MyFile.csv', [mv1], '-append');
% this goes less frequently, I presume
filename = [datestr(now,30),'.csv']; % dynamic filename
dlmwrite(filename, MyValue1);
end
This way you have both your continuously logging file (updated every minute), and a series of smaller files containing what were the last 60 values at the time they were written (updated hourly, or on some other trigger, as required).
With your timer, one way of doing this would appear to be to keep a simple counter of how many times the acquisition script has run and then use the mod function to check for when this hits a multiple of 60.
This is not a full answer, but I do have an idea if I understand you correctly. This means you probably want to run Matlab 24/7? Or at least non-stop for a certain amount of time? If so, you could try out the command clock, it shows and stores the system time. In your case
time=clock;
where
time(4) holds the hour. So as soon as this parameter changes, you should open your .csv file and save the last 60 values.
However, doing it this way I think is highly energy-consuming. And sadly Matlab does not have a sleep command like for example Unix does, so maybe it would be interesting to look into running this program in another programming language?
Please provide me with comments and feedback since I see that this is not a complete answer (yet)!

Get the time elapsed for file created time and current time in powershell script

I am trying to find out whether the file in the folder existed for morethan 30 minutes or not.I need find the difference between current system time and file created time in powershell.Please help m eout
This will return the minutes since the files had been copied.
$($(Get-Date) - $(gci filename).CreationTime).Minutes