LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   this script has been run N times since last host system reboot (https://www.linuxquestions.org/questions/programming-9/this-script-has-been-run-n-times-since-last-host-system-reboot-4175674925/)

Skaperen 05-09-2020 08:20 PM

this script has been run N times since last host system reboot
 
i want to determine how many times a script has been run since the last reboot. if two instances are started at nearly the same time, getting swapped numbers is OK because the goal is to have them be unique, especially which process gets the number 0. this needs to be done in bash (even in ".bashrc"), but it could invoke something written in another language (open source is essential), passing its name.

this is in the scope of each user. running the same script in a different user gets its own sequence of numbers starting at 0.

this is the first idea.

i'd start by making sure only one process of this script can run at one time by locking on "~/.name_of_script.lock". then i'd see if "~/.name_of_script.runs" already exists. if so, i'd read the number in it, add 1, and set N to the sum. if not, i'd set N to 0. then i would store N into "~/.name_of_script.runs". then i would unlock "~/.name_of_script.lock".

any better ideas? i'm thinking of making a module/command to do this in python and just using that from bash.

NevemTeve 05-10-2020 12:00 AM

Put your counter-file (the data-file that contains the counter) into /tmp -- The whole content of /tmp is deleted at reboot.

If you want synchronization, you can use `flock` program.

Otherwise it is easy to do in bash, please make some effort on your own.

rnturn 05-10-2020 12:35 AM

Quote:

Originally Posted by Skaperen (Post 6121259)
i want to determine how many times a script has been run since the last reboot. if two instances are started at nearly the same time, getting swapped numbers is OK because the goal is to have them be unique, especially which process gets the number 0. this needs to be done in bash (even in ".bashrc"), but it could invoke something written in another language (open source is essential), passing its name.

this is in the scope of each user. running the same script in a different user gets its own sequence of numbers starting at 0.

this is the first idea.

i'd start by making sure only one process of this script can run at one time by locking on "~/.name_of_script.lock". then i'd see if "~/.name_of_script.runs" already exists. if so, i'd read the number in it, add 1, and set N to the sum. if not, i'd set N to 0. then i would store N into "~/.name_of_script.runs". then i would unlock "~/.name_of_script.lock".

any better ideas? i'm thinking of making a module/command to do this in python and just using that from bash.

Unless there's a real need to prevent simultaneous executions, a simpler (IMHO) solution might be to tweak the script to issue a logger(1) command with a message containing the script name and the name of the user account running it. Your reporting would just require writing a script to comb through the log file looking for those logger messages to collect user names and accumulate execution counts.

HTH...

Skaperen 05-13-2020 05:09 PM

Quote:

Originally Posted by NevemTeve (Post 6121290)
Put your counter-file (the data-file that contains the counter) into /tmp -- The whole content of /tmp is deleted at reboot.

i love this solution. i don't have to check timestamps.


All times are GMT -5. The time now is 04:13 PM.