LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2020, 08:20 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
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.
 
Old 05-10-2020, 12:00 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.

Last edited by NevemTeve; 05-10-2020 at 12:01 AM.
 
1 members found this post helpful.
Old 05-10-2020, 12:35 AM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,804

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by Skaperen View Post
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...
 
Old 05-13-2020, 05:09 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: A lot has changed since Android 11 was but a twinkle in Google's eye – so mobile OS has been delayed a month LXer Syndicated Linux News 0 05-08-2020 04:24 AM
LXer: Linux users: How long has it been since you last used Windows? LXer Syndicated Linux News 3 09-09-2018 05:43 PM
[SOLVED] Old Dell 700M laptop, used to run 10.2 well, has not been good since. danbud Linux - Newbie 9 10-08-2012 01:38 PM
[FYI] "last -f <old wtmp> -x reboot shutdown" incorrect for last reboot and shutdown catkin Linux - General 1 03-25-2010 11:52 PM
i have been getting an error since last few startups like"Sound server informational amolgupta Linux - Software 2 04-24-2006 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration