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
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
You could have the script create a lockfile. It will need some way to determine if it's been run before.
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?
Since a shellscript has access to it's PID through $$, one could do something like
#!/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.
[later] forgot demonstration:
$ ./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
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:
script_name=try.sh
ps -ha | grep "$script_name" | grep -v grep
hth
ps -ha | grep [t]ry.sh