ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I have an entry in my crontab as follows:
0,15,30,45 * * * * cd /home/user/seti; ./setitime.sh > /dev/null 2> /dev/null
It is set to launch a script (setitime.sh) that logs the time it takes my machine to process seti@home packets.
The script is as follows:
#!/bin/bash
# display the duration in a logical string
duration ()
{
totalsecs=$1
secs=$(($totalsecs % 60))
mins=$((($totalsecs % 3600) / 60))
hours=$(($totalsecs / 3600))
if [ $hours = 0 ] && [ $mins = 0 ]
then
result_str="$secs second(s)"
elif [ $hours = 0 ]
then
result_str="$mins minutes(s) $secs second(s)"
else
result_str="$hours hour(s) $mins minute(s) $secs second(s)"
fi
echo $result_str
}
# clean up old files
#rm -f time
touch setitime
echo "Seti packet started at `date`" >> setitime
START_DATE=`date +%s`
#Do some stuff
./setiathome -nice 1 -stop_after_process
In normal operating mode multiple instances of setiathome will not run if an instance of setiathome is already running.
Is there a way to prevent multiple instances of my script from running?
The way I would like this to work is:
- cron launches setitime.sh.
- setitime.sh logs the start time to setitime, launhes setiathome (with the option to stop after processing)
- setiathome stops processing, setitime.sh logs the stop time to setitime.
- setitime.sh stops
- cron launches setitime.sh
- (repeat)
If cron attemps to start a second instance of setitime.sh, it aborts.
Here's what's happeneing:
- cron launches setitime.sh.
- setitime.sh logs the start time to setitime, launhes setiathome (with the option to stop after processing)
- 15 minutes pass and cron launches a second setitime.sh
- setitime.sh logs the start time to of the second seti@home to setitime.
- can't run 2 setiathome. second instnace of setiathome aborts.
- setitime.sh logs the stop time of 2nd setiathome to setitime.
- setitime.sh logs the duration of 2nd instance of setiathome to setitime (0 seconds)
- (repeat from step 3 every 15 minutes)
Any thoughts on how to only allow 1 instance of my bash script?
OK...
i've played around with this for a ilttle while. what I really need is for the test to pass when it DOES NOT find an instance of itself already running.
I've put this in a test script this works when setitime.sh is running.
#!/bin/bash
if [ `ps aux|grep -c setitime.sh` ]; then
echo "setitime.sh running"
fi
I need something like
#!/bin/bash
if [ `ps aux|grep -c setitime.sh` -eq 0 ]; then
echo "setitime.sh is not running"
fi
the above does not seem to work, nor have any variants I've tried.
Tried both variations. Neither worked. I finally got this to do what I want:
#!/bin/bash
foo=`ps aux | grep -c setiathome`
#echo $foo
if [ "$foo" -eq "0" ]; then
echo "setitime.sh is not running"
fi
The beauty of Linux... more than one way to skin a cat.
I wrote functions just for that. ps always seems to exit on 1 on my machine here so I had to come up with another way. All you need yo do is feed getPID the script name you are looking for.
@sayanarijit:
You have replied to a 15 year dead thread, and although your script may be relevant you did not provide any description of your solution other than a link and command example.
If you wish to share your solution with current users please post to your own thread and provide relevant detail so that your post is complete and more useful to others.
Please see the Site FAQ for guidance in posting and forum participation.
And welcome to LQ!
Last edited by astrogeek; 01-12-2018 at 01:02 PM.
Reason: typos
has a race condition; the grep command can immediately appear in the ps list and then grep finds itself and matches in its arguments.
Correct is, in the grep argument enclose (at least) one character in [ ]. Then it still searches the bare character, but the [ ] will appear in the ps list and will not match.
Because [ ] is special to the shell, the grep argument must be put in quotes.
In this case there is another glitch: the . matches any character, and should be escaped \. or put in a character set [.].
Take the latter - it also solves the race condition!
Last but not least, one can use the exit status, saves a subshell.
Code:
if ps aux | grep -wq "setitime[.]sh"; then
(This is even true after 15 years.)
Last edited by MadeInGermany; 01-13-2018 at 02:27 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.