Bash script to count number of executions instances does not work.
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Number of detect_itself.sh instances running now = 2
Second method:
1
Third method:
2
Please, press a key
But I expected it to show:
Number of detect_itself.sh instances running now = 1
Second method:
1
Third method:
1
Please, press a key
I do not understand why if I execute ps -ef | grep detect_itself.sh | grep -v -i grep | wc -l it returns the value 1, but if I save this value in a variable and show it with echo it shows 2.
Last edited by jose_spain; 05-30-2019 at 08:02 AM.
Looking at this and thinking what good, or real world usage could this possibly be used for, a script that checks itself to see if it has another one running or not, if yes how many?
then I remember something I wrote in a script long long ago to see if more than one (child) was running. this is a snippet of it. Maybe (hopefully) you can figure out how to do something with it to get what you're looking for. It actually goes in line with what pan64 is telling you, you need to use.
A classic usage example is a cron job that runs so frequently that successive runs may overlap if eg the amt of incoming data grows enough (& it is designed to single task).
There are a few ways of checking this situation, but checking the process table is more reliable/accurate than lock files - I've seen that fail a few times.
The backticks spawn a sub shell. Dependent on your shell interpreter, it can put another process in the proctable that can match your grep.
As suggested already, pgrep is the way out.
in recent bash versions, neither wc nor pgrep are builtins.
OP seems to have gone AWOL.
in any case, i use this in one of my scripts:
Code:
function only_me_or_exit {
# argument: pidfile
# make sure only 1 instance is running
touch "$1"
read lastPID < "$1"
# if lastPID is not null and a process with that pid exists , exit
[ ! -z "$lastPID" -a -d /proc/$lastPID ] && { echo "An instance of $me is already running with pid $lastPID." ; exit 1 ; }
# else - save my pid in the lock file, and continue
echo $$ > "$1"
}
been a while since i wrote it, code might be inferior.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.