PS finds a ghost?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers PS finds a ghost?
# 1  
Network

Hello,

I have problems executing a script in ksh with this script named
process.sh:
Code:
ps -ef | grep process.sh | grep -v grep | wc -l | read a
if [ $a -gt 1 ]
then
 echo "The script is running" 
exit 0
fi

The problem is that when I execute the script, sometimes it shows the
message "The script is running", when I think it's impossible.
The reason that I write "wc -l|read a" is that
the count returns 1 because the "ps" is executed in the same script.
My idea is that nobody execute 2 times the same script, only on time.

Is something wrong?

Thanks in advance.

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 05:40 PM..
# 2  
How about:
Code:
#!/bin/ksh

ps -ef | grep "[p]rocess.sh" 1> /dev/null 2>&1
if [$? -eq 0]
then
	echo "script is running"
	exit 0
fi
exit 1

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 05:40 PM..
# 3  
Data

The script is always running 1 time, I must insert the code in the same script to control if someone will execute the script again.
# 4  
Ok, I understand now. Why don't you make the script test for a lockfile when it starts?

if [ -a /tmp/process.lock ]; then
exit
fi

touch /tmp/process.lock
# rest of script
rm /tmp/process.lock
# 5  
Your idea is good, but I need to implement it in a lot of scripts (77 aprox), a lot of them running at the same time.

What I don't want is to use 77 different names for each temporary file.

# 6  
Ok, third time's a charm...

What about:
Code:
#!/bin/ksh
NUM=`ps -ef | grep "[p]rocess.sh" | wc -l
if [ $NUM -gt 1 ] 
then 
            echo "The script is running" 
            exit 0 
fi

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 05:40 PM..
# 7  
Quote:
#!/bin/ksh
NUM=`ps -ef | grep "[p]rocess.sh" | wc -l
Script is perfect.

But it lost closing quote. isn't it?
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #413
Difficulty: Medium
The AT&T 3B1, also known as the PC7300 or UNIX PC, was a desktop workstation computer based on the Motorola MC68010 microprocessor, running an operating system from Convergent Technology (based on Unix System V Release 2).
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command finds some, misses some

The contents of my home directory: bin Desktop Documents Downloads folders Music Pictures Public Templates Videos When I run the command for file in /home/myself/*d*; do if ; then echo $file; fi; doneit finds /home/myself/Downloads /home/myself/Videos but not "folders". ... (5 Replies)
Discussion started by: Xubuntu56
5 Replies

2. Shell Programming and Scripting

Finds all duplicate files

Hi, How would you write bash script that given a directory as an argument and finds all duplicate files (with same contents - by using bytewise comparison) there and prints their names? (6 Replies)
Discussion started by: elior
6 Replies

3. Shell Programming and Scripting

I want my script to NOT to send an e-mail if it finds the same keyword more than twice.

My script triggers and e-mail if keywords supplied to it were found. Problem is if it find the same keyword continously (due to continous server errors), it triggers mails and fillup my mail box with same message (which is not required) I want my script to NOT to send an e-mail if it finds the... (13 Replies)
Discussion started by: Rajeshneemkar
13 Replies

4. Shell Programming and Scripting

Ksh: Send a mail in case grep finds something

I want to search a file if it contains special strings and if yes, the records found should be mailed. I can either do it with a temporary file: /usr/bin/grep somestring somefile > /tmp/tempfile && /usr/bin/mail -s "Found something" email@mycomp.com < /tmp/tempfile... or by running the grep... (10 Replies)
Discussion started by: Cochise
10 Replies

5. Shell Programming and Scripting

sed finds nothing but it changes file's timestamp

I must go through some files to change a certain string within text files to another string. I use openSUSE and folders are mounted by cifs. Text to be replaced (only in .m extension) is U:\FOLDER and new string is N: That works fine with spaces in directory names etc., but this process ... (5 Replies)
Discussion started by: Pappa41
5 Replies

6. UNIX for Dummies Questions & Answers

find -size -7M finds files, but won't cp them all

If I run: find /somefolder -type f -size -7M | wc -l I get 73594 files But when I run find /somefolder -type f -size -7M -exec /bin/cp -v {} /someotherfolder/ \; it only copies 38891 of the files to the folder, why? There's a mix of all types of files in /somefolder. Is there some other... (12 Replies)
Discussion started by: unclecameron
12 Replies

7. Shell Programming and Scripting

combine multiple finds into 1-liner

How to combine 3 find statements into 1-liner? find statements: cd ${dir1} ; find . ! -name . -prune -type f | xargs file | grep -i ascii | cut -f1 -d: | xargs grep -l "${searchtxt}" cd ${dir2} ; find . ! -name . -prune -type f | xargs file | grep -i ascii | cut -f1 -d: | xargs grep -l... (4 Replies)
Discussion started by: ux4me
4 Replies

8. Shell Programming and Scripting

Can sed replace every 2 instances it finds in a file? Pattern.

My goal is to make a script to find/replace the variable "PORT" with a unique number. Like the following <VirtualHost 174.120.36.236:PORT> ServerName architect.com.ph ServerAlias www.architect.com.ph DocumentRoot /home/architec/public_html ServerAdmin... (16 Replies)
Discussion started by: EXT3FSCK
16 Replies

9. News, Links, Events and Announcements

SETI Finds Stolen Laptop

The SETI@home project invites volunteers to install special software of their home computers. This software, called BOINC, runs computational jobs when the computer would otherwise be idle. And the software needs to contact the main server periodically to deliver results and obtain new problems.... (2 Replies)
Discussion started by: Perderabo
2 Replies

Featured Tech Videos