stop execution of script with in the script


 
Thread Tools Search this Thread
Operating Systems AIX stop execution of script with in the script
# 1  
Old 03-20-2008
stop execution of script with in the script

Hello

i want to incorporate of piece of code in the shell script which checks whether same script is already running or not. If it is already running i want to come out (exit) if its not then continue with the execution of the script.

Any help would be very much appreciated

Thanks
# 2  
Old 03-20-2008
Hammer & Screwdriver A couple of approaches to solve...

(a) do a >ps -ef | grep script_name | wc -l
If you see three matches, then already running. (#1 is current execution, #2 would be the grep command, and #3 would be another execution)
(b) whenever you start the script, have it >touch script_name.on
Then, you can do a >if [ -e script_name.on ] to see if the file (and thus the process) is already going. Just remember to >rm script_name.on at the end of your program.
# 3  
Old 03-20-2008
Try this:

https://www.unix.com/shell-programmin...html#post92287

Note that you can use the variable $0 within a bash script to get the script's own name.

ShawnMilo
# 4  
Old 03-20-2008
Same idea as the other replies... Don't know what flavor of unix you are on, but on Solaris, ps -fo 'pid= args=' will list processes (for a user) giving only their PID and the argument list (including the script name) this might make it easier to parse through the results and look for your own proc.

I have a perl subroutine that does exactly what you want, though it is a little complex cause it does all kinds of logging, etc if the procname and userID passed to it do not exist, etc. But bottom line is execute a ps, capture the results and count the lines containing it...

Also, as the last reply said, you can set a flag (touch a file) that signals the proc is active (each run must check to see if the file already exists). You can also use semaphors (if your unix supports sys V IPC calls) to do the same thing.... I can send you the perl routine if you want....

quine@sonic.net
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Stop script execution, when we encounter error

Team, Im new to shell scripting please help in below issue: Considering scenario i have 1.sql,2.sql,3.sql and 4.sql . If my second script fails my loop should come out and should not execute 3.sql and 4.sql. Here is my code. #! /bin/bash echo "exit" | user/password | grep Connected >... (4 Replies)
Discussion started by: preethi87
4 Replies

2. Shell Programming and Scripting

To stop execution in cron

hi guys i have a question my cron should start executing minute but it sould stop execute only i have tried tis 10 * * * * /home/sample.sh >> /data/band/cron_$(date+|%Y|m|d).log (2 Replies)
Discussion started by: azherkn3
2 Replies

3. Solaris

Script on Solaris spawning 2 processes for one shell script execution

Hi, I am having a shell script on Solaris 10 which has a while loop as shown below. #!/usr/bin/ksh # while do sleep 60 done Name of the shell script is coldcentric.sh. I executed script /DATAWAREHOUSE/LOAD/Scripts/coldcentric.sh from a command task in Informatica worklow as... (3 Replies)
Discussion started by: chekusi
3 Replies

4. Shell Programming and Scripting

Stop child script by stoping parent script

Hi everyone, I have this problem with a script I'm writting. I want to execute a code running in the background several times through a script. I am writting it like that parent_script for a in 1 2 3 4 5 do exec test -n $a done What I want to do is when parent_script is killed,... (0 Replies)
Discussion started by: geovas
0 Replies

5. Emergency UNIX and Linux Support

invoke one script based on previous script execution

I am database guy and not very good at shell scripts. I am seeking help to sharp my script coding. I have 5 scripts 1. master script. I use this one to call other four scripts to do database work. 2. db_backup_1 and log_backup_1 3. db_backup_2 and log_backup_2 in master script, I want to... (4 Replies)
Discussion started by: duke0001
4 Replies

6. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

7. Shell Programming and Scripting

Stop execution of script if some condition fails

After my if condtion give rusult true then script should stop execution. Please advice...Thnaks in advance (1 Reply)
Discussion started by: vivek1489
1 Replies

8. Shell Programming and Scripting

how to stop execution of a script after one hour

I have a shell script that writes some data in a file. I want to stop the script after one hour from start of execution using "EXIT 1". how to do it. I don't want to use CRONTAB. (5 Replies)
Discussion started by: mady135
5 Replies

9. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

10. Programming

how to stop execution in for loop

Hi all, I am working on a c source code nearly 2000 line . it contains one big for( i=0; i< 200 ; i++ ) loop of around 600 lines could any tell me how to break the execution of prog when the value of i is 50 in for loop so that i can check inside the loop. Thanks.. (1 Reply)
Discussion started by: useless79
1 Replies
Login or Register to Ask a Question