I'm looking to be able to print the amount of times a file has been ran. Is there a way to do that?
I'm looking to be able to print the amount of times a file has been ran. Is there a way to do that?
Yes, but only under very specific circumstances.
The first condition is that for most operating systems your program will need to be running for the entire duration that you are observing. The easiest way way to do this is by probably by using the notify crate to listen into file-system events. However, this also has some issues. If the file you are observing is being opened very quickly many times in a row, you may over or under count the number of actual occurrences. The reason being is that exactness is not really a major concern. Since nearly all use-cases for these apis relate to detecting if/when changes occur to files or directory structures so that some action can be performed using the new file system state.
You can still achieve accurate results by other methods, but due to how rare this use case is, there is not much in the way of libraries or documentation to help you. You can expect this approach to involve monitoring syscalls and be comprised almost entirely of unsafe code.
On the other hand, the way you word your question makes me wonder if you are not looking to detect files being opened, but number of times an executable is being run. Depending on what executable you are referring to and how it is used, this can be way easier to do. If the executable is one that you wrote, then the problem is pretty much solved from the get-go. You can just add some extra code to open a file and increment a number for the number of times your program was executed. On the other hand if it is a simple standalone binary, you may be able to get away with just renaming it and replacing it with a program that records when it is executed then runs the intended program. Some troubleshooting may be required depending on how other programs interact with this executable. For example, there are a number of linux utilities which are covered by a single executable, but function differently based on their name (Ex: sudo vs sudoedit).