Check whether shell script has started.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check whether shell script has started.
# 1  
Old 07-30-2015
Check whether shell script has started.

How can i ceck as shellscript, if a other shellscript has been started?

The other script can bee started by a other user.
The task will not run twice
# 2  
Old 07-30-2015
You could have the script create a lockfile. It will need some way to determine if it's been run before.
# 3  
Old 07-30-2015
If executed in a subshell, ps will show it as a parameter to your shell, so you can check. What do you mean by "twice"? Two instances in parallel, twice during an hour, a day, a week?
# 4  
Old 07-30-2015
Since a shellscript has access to it's PID through $$, one could do something like

Code:
#!/bin/sh                                                                      

echo my pid $$
echo my name $0
if ( ps -ef | grep -v grep |grep "/bin/sh $0" | grep -v $$ ) ; then
   echo somebody else is running  $$ cancelling
   exit
fi

echo me running, continuing with execution

while (true) ; do
  continue
done
<continue with script>

The test in the if statment has the "added?" benefit of returning which processes are interfering with your own instance.

But that's today's brain-teaser for me. Smilie

[later] forgot demonstration:
Code:
$ ./try.sh &
[2] 10191
my pid 10191
my name ./try.sh
me running, continuing with execution

$ ./try.sh
my pid 17374
my name ./try.sh
<username>   10191 26900 30 12:56 pts/2    00:00:01 /bin/sh ./try.sh
somebody else is running 17374 cancelling


Last edited by featheredfrog; 07-30-2015 at 01:56 PM.. Reason: forgot proof of concept:
# 5  
Old 07-31-2015
Since each call of a new script, specialy if by another user, gets another pid, this method is not very reliable.

Instread i'd check:
Code:
script_name=try.sh
ps -ha | grep "$script_name" | grep -v grep

hth
# 6  
Old 07-31-2015
Quote:
Originally Posted by sea
Since each call of a new script, specialy if by another user, gets another pid, this method is not very reliable.

Instread i'd check:
Code:
script_name=try.sh
ps -ha | grep "$script_name" | grep -v grep

hth
ps -ha | grep [t]ry.sh
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

2. Shell Programming and Scripting

how to check whether a process started at particular time

I want to check whether a particular process has started at 10:00a.m or not. I can check process by ps -fu but dont know how to check it with respect to time. Could anyone help me with this? ---------- Post updated at 11:14 AM ---------- Previous update was at 10:52 AM ---------- can i use... (9 Replies)
Discussion started by: kishore kumar
9 Replies

3. Shell Programming and Scripting

help on shell script to check line

Hi Scripting Gurus, Can someone help to transform the below logic into a shell script, might be easy for some of you. I have a file with below text, I need if the line has the ":" and the above to it is not a blank line should print " <text>: is incorrect format" Apple: ... (3 Replies)
Discussion started by: usyseng
3 Replies

4. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

5. Shell Programming and Scripting

Shell script to check numbers!

Hello All, I have 3 types of files. The names of which starts with P,I,M like P********* D********* M********* now I need to do some operations witht hese files.. so if file name starts with P or p then do the operation for P file... fi else (20 Replies)
Discussion started by: smarty86
20 Replies

6. AIX

How to check a script was started using 'sudo' ?

How can I from within a script, find out if that script was started using 'sudo' and by a valid soduer ? (1 Reply)
Discussion started by: Browser_ice
1 Replies

7. Shell Programming and Scripting

Different time format in script, started in shell or in cron

I try to write a python script, which analyze user logon time with "who" command. When i start script in bash, i get this result: USER=mnadmin tty7 2009-04-18 11:37 (:0) But when i start script in cron, i get result like this: USER=mnadmin tty7 Apr 18 11:37 (:0) I see -... (2 Replies)
Discussion started by: jrush
2 Replies

8. Shell Programming and Scripting

IP check with shell script

hi guys ..newbie here i would like to create a simple script tat will detect wether the machine has IP or not ... but it seems that my script doesnt really work it kept rebooting... i set this script during boot so that it will check else it will reboot it a shell script thou... ... (5 Replies)
Discussion started by: bladez
5 Replies

9. Shell Programming and Scripting

check my first shell script

I have written the below script to determine whether a string is palindrome or not ? But its not working, any help to debug it ? I am new to this forum but when I searched for my question, I found that many people refused to answer this question thinking that its Homework question, Therefore I... (2 Replies)
Discussion started by: gridview
2 Replies

10. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies
Login or Register to Ask a Question