code checking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting code checking
# 1  
Old 09-22-2011
code checking

i was just wondering how would you check , beside the lock method, if an instance of another code is already running and if it is then output a message to the user saying the program is already running and exit!! the code is in BOURNE SHELLL!!!

thanks in advance!!
# 2  
Old 09-22-2011
Code:
# prgname='test.sh'
# cat $prgname
sleep 120
# sh test.sh  &                   
[1]     25806
# if [ "$(ps -ef|awk '/'${prgname}'/ && !/awk/')." != "." ]
> then
> echo "${prgname} running !"
> fi
test.sh running !

# 3  
Old 09-23-2011
Quote:
Originally Posted by Klashxx
Code:
# prgname='test.sh'
# cat $prgname
sleep 120
# sh test.sh  &                   
[1]     25806
# if [ "$(ps -ef|awk '/'${prgname}'/ && !/awk/')." != "." ]
> then
> echo "${prgname} running !"
> fi
test.sh running !

thanks for replying, but i dont quite understand what you did!! i tried running it too and it doesnt work!!
plzzz help!!


thanks
# 4  
Old 09-23-2011
Code:
# prgname='test.sh' # cat $prgname sleep 120 # sh test.sh  &                    [1]     25806

That part is telling ya to assign the script name to the variable prgname, what it does (the cat part) and he's executing it in background (&). You can see the process id of the script at the end (25806).

He's just showing you that the script is already running so you can test it.

Code:
# if [ "$(ps -ef|awk '/'${prgname}'/ && !/awk/')." != "." ] > then > echo "${prgname} running !" > fi test.sh running !

That part is the actual loop thats looking for the running script. [ ] Tests if the script is already running. If it does it echo's "(script name) running!". The last part is the actualy the result of the command which tells you ... yeah your script is running.

Back a bit... the test part.

[ ] this is the test part. So if [test] is true do something.

Test what? If the variable exist. If it exists you want to know.

ps -ef will list the processes (good thing to know if your looking for a process).

He's pipping it into a awk which will search for the variable prgname BUT will leave out awk. You always want to do this cause else it will always return with your actual search. See ex:

Code:
$ ps -ef | awk '/'ssh-agent'/'
user     1625 25793  0 10:55 pts/2    00:00:00 awk /ssh-agent/
user    20491     1  0 Sep19 ?        00:00:00 ssh-agent

You want the second one but not the first one. So the AND operator (&&) is really needed here to skip your search.

Ok ... last thing he does ... he assigns the result to a variable PLUS a dot "$(bla bla bla bla)."

See the trailling dot at the end? Thats actually needed incase your result is empty. If it is your variable will be a simple dot. That simple dot will be compared the other part of the test "."

That != says NOT equal so if it finds your script name that test will look like:

test.sh. (see the dot at the end of test.sh) NOT equal to dot

That test is true so do somehing.

In case its not running the test will look like this:

"." NOT equal to "." which is not true so it will skip the "do something part"

Hope it helped. Thats really the simplest i can do.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Code for checking if certain no of files exists

Hi, I am writing the shell script in ksh to check certain no of files exists,In my case there are 7 files exist like below Sales1_timstamp.csv Sales2_timstamp.csv Sales3_timstamp.csv Sales4_timstamp.csv Sales5_timstamp.csv Sales7_timstamp.csv Sales7_timstamp.csv Once all the files... (4 Replies)
Discussion started by: SRPR
4 Replies

2. Shell Programming and Scripting

Checking LB status.. stuck in script syntax of code

#!/bin/ksh #This script will check status of load balancer in AIX servers from hopbox #Steps to do as folows : #Login to server #netstat -ani | grep <IP> #check if the output contains either lo0 OR en0 #if the above condition matches, validation looks good #else, send an email with impacted... (7 Replies)
Discussion started by: vinil
7 Replies

3. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

4. Shell Programming and Scripting

Checking for duplicate code

I have a short line of code that checks very rudimentary for duplicate code: sort myfile.cpp | uniq -c | grep -v "^.*1 " | grep -v "}" It sorts the file, counts occurrences of each line, removes single occurrences and removes the ubiquitous closing brace. The language is C++, but is easily... (3 Replies)
Discussion started by: figaro
3 Replies

5. Programming

code for checking username char is not present in password.

hi friends , i need c code for my app, in my app the user can create the new user for the system, while creating the new user ,i try to include the condition the user name is not present in password. my exe requrment is : the user name char is not present in password,if the password... (6 Replies)
Discussion started by: vasu28
6 Replies

6. Shell Programming and Scripting

checking the return code

hi i have a file, i am reading line by line and checking a line contains a string , `grep "Change state" $LINE` if then echo "The line contains---" else echo "The line does not contains---" i need to check the return code , but i am getting an error ... (4 Replies)
Discussion started by: Satyak
4 Replies

7. Shell Programming and Scripting

Error code checking

I'm trying to create a directory from my Perl script. Only if the there was an error I want to let the user know about it. So if the folder exists is ok. This is what I think should work: `mkdir log 2>/dev/null`; if($? == 0 || $? == errorCodeForFileExists) { everyting is fine } else {... (3 Replies)
Discussion started by: jepombar
3 Replies

8. Shell Programming and Scripting

Code checking for all values in the same if statement.

I am trying to set up a variable based on the name of the file. function script_name { if then job_name='MONITOR' return job_name; elsif then job_name='VERSION' return job_name fi } for i in `ls *log` do script_name $i done. (4 Replies)
Discussion started by: oracle8
4 Replies
Login or Register to Ask a Question