[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: shell script problem
[warning: a slightly longish explanation, with a proof]
> I killall pnserver.
> When it "does" something it starts pnserver.
> When it "doesn't" it doen't start the pnserver.
>
> I moved the script into a different folder, and now it seems to work from
> everywhere. Elohim Gadol (both folders have the same permissions and
> ownership! The script included full paths to all binaries!).
Elohim may be gadol, but your problem is katan katan U'mitchabe...
lets look at your script again:
> #!/bin/sh
> numps=`ps xa | grep pnserver | wc -l`
> if test $numps -lt "2"
> then
> /usr/local/pnserver/bin/pnserver /usr/local/pnserver/server.cfg
> fi
now, lets see what we get when we run this script. to do that, we add the
oh-might '-x' flag to the bang line:
#!/bin/sh -x
this causes the script to show each line before executing it. now, running
from the same directory as where the script is:
---------------------------------------------------------------
[root@p16 pnserver]# ./checkpn
++ ps xa
++ grep pnserver
++ wc -l
+ numps= 1
+ test 1 -lt 2
+ /usr/local/pnserver/bin/pnserver /usr/local/pnserver/server.cfg
-----------------------------------------------------------------
this looks fine. what happens if i switch to another directory and then
run the script:
-------------------------------------------------------------------
[root@p16 pnserver]# cd ..
[root@p16 local]# ./pnserver/checkpn
++ ps xa
++ grep pnserver
++ wc -l
+ numps= 3
+ test 3 -lt 2
--------------------------------------------------------------------
a-ha! now the process count got up to 3. how come? can you see the
pattern?
lets add the following line into the script, right before the original
'ps' line:
ps xa | grep pnserver
and run it again:
-----------------------------------------------------------------
[root@p16 local]# ./pnserver/checkpn
+ ps xa
+ grep pnserver
498 5 S 0:00 sh -x ./pnserver/checkpn
500 5 S 0:00 grep pnserver
++ ps xa
++ grep pnserver
++ wc -l
+ numps= 3
+ test 3 -lt 2
-----------------------------------------------------------------
now, you see why we got a count of '3'? when you ran the script with a
relative (of full path), the path contained the name of the directory,
which accidentally matches the name of your server's binary...
when you moved the script to another directory, it no longer contained
'pnserver' on the command line...
Q.E.D
guy