Register
It is currently Sat Dec 20, 2014 10:20 pm

Loops Create New Processes?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Nov 14, 2011 1:14 pm   

Joined: Mon Nov 14, 2011 1:08 pm
Posts: 2
I'm confused. I have created a script called aslkgsdklh to ensure that it's name won't match any other running processes. If I execute this script only once, why would this code ever return anything other than ones?

Code:
#!/bin/bash
while [ 0 -lt 1 ]; do
        echo $(ps -A | grep -v defunct | grep -c $(basename $0))
done


Thanks,
=-Jameson


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 5:06 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
See the difference between
Code:
ps -A | grep bash

and
Code:
echo $(ps -A | grep bash)

or, even
Code:
echo $(ps -A | grep bash) | cat


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 7:09 pm   

Joined: Mon Nov 14, 2011 1:08 pm
Posts: 2
What I I'm actually trying to accomplish is:
Code:
if [[ $(ps -A | grep -c $(basecode $0) -gt 1) ]]; then
     exit 1
fi


I know I've used something similar before, but that explains why it's not working. Thanks.


Top
 Profile  
 PostPosted: Tue Nov 15, 2011 11:36 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
What you're trying to do is called mutual exclusion. It rejoins this topic.

So, you can use a lock file and flock(1).

Code:
#!/bin/bash

exec 3> /tmp/my_super_script_name.lock

if ! flock -n 3; then
    echo 'error: another instance is running.' >&2
    exit 1
fi

# script's code goes here and beyond...


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP