Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sleep will succeed while wait will fail.

18 views
Skip to first unread message

Hongyi Zhao

unread,
Apr 2, 2015, 9:16:15 AM4/2/15
to
Hi all,

I use the open source vpn client, named vpngate -- see here for detail
http://www.vpngate.net/ -- to access some websites under my Debian Wheezy
box.

This softerware has a command line management utility, i.e., vpncmd
command. Which let the users, especially the ones under *nix os's to do
the management jobs with the shell scripts.

The following is a simple script which do some setting jobs for the
client side -- I've give some comments on each line of these commands:

--------------
# Start the vpngate client service, so that the vpncmd
# can connect to it for doing some management jobs:
sudo vpnclient start
# Connect to vpngate client service running on the localhost,
# and create a virtual network adapter named nic:
sudo vpncmd /client localhost /CMD niccreate nic
# Delete the virtual network adapter named nic:
sudo vpncmd /client localhost /CMD nicdelete nic
# Stop the vpngate client service:
sudo vpnclient stop
--------------

The issue is, when I running the above commands from a script, I'll meet
the errors as follows -- I've used the script file test-vpngate.sh which
includes the above commands:

---------------
werner@debian:~$ bash test-vpngate.sh
The SoftEther VPN Client service has been started.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Error occurred. (Error code: 1)
Connection to the server failed. Check network connection and make sure
that address and port number of destination server are correct.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>nicdelete nic
NicDelete command - Delete Virtual Network Adapter
Error occurred. (Error code: 29)
Object not found.
Stopping the SoftEther VPN Client service ...
SoftEther VPN Client service has been stopped.
---------------

It seems that the issues are caused by the vpngate client service
relative the first command -- this command require some times for
starting, and the following commands cann't use this service for doing
jobs. So I change the script into the following form:

---------------
sudo vpnclient start
sleep 1
sudo vpncmd /client localhost /CMD niccreate nic
sudo vpncmd /client localhost /CMD nicdelete nic
sudo vpnclient stop
---------------

And at this point, all of the commands will executed successfully:

--------------
werner@debian:~$ bash test-vpngate.sh
The SoftEther VPN Client service has been started.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>niccreate nic
NicCreate command - Create New Virtual Network Adapter
The command completed successfully.

vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>nicdelete nic
NicDelete command - Delete Virtual Network Adapter
The command completed successfully.

Stopping the SoftEther VPN Client service ...
SoftEther VPN Client service has been stopped.
---------------

Now, I change the script into the following form:

------------
sudo vpnclient start
wait
sudo vpncmd /client localhost /CMD niccreate nic
sudo vpncmd /client localhost /CMD nicdelete nic
sudo vpnclient stop
------------

And again, the script will failed to execute as follows:

------------
werner@debian:~$ bash test-vpngate.sh
The SoftEther VPN Client service has been started.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Error occurred. (Error code: 1)
Connection to the server failed. Check network connection and make sure
that address and port number of destination server are correct.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>nicdelete nic
NicDelete command - Delete Virtual Network Adapter
Error occurred. (Error code: 29)
Object not found.
Stopping the SoftEther VPN Client service ...
SoftEther VPN Client service has been stopped.
------------

So my issues are as follows:

1- Why sleep can let the script executed successfully, while the wait
cann't do the trick?

2- If I want to use a more precising time for sleeping, say, to execute
all of the subsequent commands immediately the vpngate client service
been started. How should I revise my commands? And is this possible?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Barry Margolin

unread,
Apr 2, 2015, 11:15:24 AM4/2/15
to
In article <mfjfeq$pfa$1...@aspen.stu.neva.ru>,
The purpose of "wait" is to wait for background jobs to finish. You
didn't start any background jobs, so there was nothing to wait for.

Background jobs are commands that are run with "&" at the end, e.g.

sleep 10 &
wait

> 2- If I want to use a more precising time for sleeping, say, to execute
> all of the subsequent commands immediately the vpngate client service
> been started. How should I revise my commands? And is this possible?

Figure out if there's some command you can run to tell whether the VPN
client has started, and perform this test in a loop until it succeeds.
For instance, if it creates a file, you could do:

while ! test -e /path/to/file; do
sleep 1
done

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Hongyi Zhao

unread,
Apr 2, 2015, 7:06:01 PM4/2/15
to
On Thu, 02 Apr 2015 11:15:21 -0400, Barry Margolin wrote:

> The purpose of "wait" is to wait for background jobs to finish. You
> didn't start any background jobs, so there was nothing to wait for.
>
> Background jobs are commands that are run with "&" at the end, e.g.
>
> sleep 10 &
> wait

According to your above notes, I use the following revising version of
these commands to test but still failed:

werner@debian:~$ cat test-vpngate.sh
sudo vpnclient start &
wait
sudo vpncmd /client localhost /CMD niccreate nic
sudo vpncmd /client localhost /CMD nicdelete nic
sudo vpnclient stop


werner@debian:~$ bash test-vpngate.sh
The SoftEther VPN Client service has been started.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Error occurred. (Error code: 1)
Connection to the server failed. Check network connection and make sure
that address and port number of destination server are correct.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>nicdelete nic
NicDelete command - Delete Virtual Network Adapter
Error occurred. (Error code: 29)
Object not found.
Stopping the SoftEther VPN Client service ...
SoftEther VPN Client service has been stopped.

Why does this happen?

Regards.

Barry Margolin

unread,
Apr 2, 2015, 7:32:53 PM4/2/15
to
In article <mfki0l$ieg$1...@aspen.stu.neva.ru>,
Hongyi Zhao <hongy...@gmail.com> wrote:

> On Thu, 02 Apr 2015 11:15:21 -0400, Barry Margolin wrote:
>
> > The purpose of "wait" is to wait for background jobs to finish. You
> > didn't start any background jobs, so there was nothing to wait for.
> >
> > Background jobs are commands that are run with "&" at the end, e.g.
> >
> > sleep 10 &
> > wait
>
> According to your above notes, I use the following revising version of
> these commands to test but still failed:
>
> werner@debian:~$ cat test-vpngate.sh
> sudo vpnclient start &
> wait
> sudo vpncmd /client localhost /CMD niccreate nic
> sudo vpncmd /client localhost /CMD nicdelete nic
> sudo vpnclient stop

Why do you think "wait" will help? Can't you tell from the original
problem that "vpnclient start" doesn't wait for the VPN to be ready
before it returns?

Like I said in my previous reply, you have to test something else that
gets changed on the system when the VPN is ready to use.

Hongyi Zhao

unread,
Apr 2, 2015, 7:58:23 PM4/2/15
to
On Thu, 02 Apr 2015 19:32:49 -0400, Barry Margolin wrote:

> Why do you think "wait" will help? Can't you tell from the original
> problem that "vpnclient start" doesn't wait for the VPN to be ready
> before it returns?

Sorry for my misunderstanding the notes.

You said that the `The purpose of "wait" is to wait for background jobs
to finish.'

So I put the running of "vpnclient start" into background, and then wait
for its finished status, and then execute the subsequent commands.

But this still cann't ensure the `vpnclient servive' has been ready for
use.


>
> Like I said in my previous reply, you have to test something else that
> gets changed on the system when the VPN is ready to use.


Ok, I've find that after the vpnclient start are ready, it will give the
following messages on the stdout:


werner@debian:~$ sudo vpnclient start
The SoftEther VPN Client service has been started.

or for the Client service has been already started:

werner@debian:~$ sudo vpnclient start
SoftEther VPN Client service has been already started.
Run the "vpnclient stop" command to stop this service.

So I use the following script to test:

--------
rm -f aaa
sudo vpnclient start | egrep '(started|already)' > aaa
if [[ -s aaa ]]
then
sudo vpncmd /client localhost /CMD niccreate nic
sudo vpncmd /client localhost /CMD nicdelete nic
sudo vpnclient stop
fi
--------

But still failed, see the following for detail:

werner@debian:~$ bash test-vpngate.sh
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Error occurred. (Error code: 1)
Connection to the server failed. Check network connection and make sure
that address and port number of destination server are correct.
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>nicdelete nic
NicDelete command - Delete Virtual Network Adapter
Error occurred. (Error code: 29)
Object not found.
Stopping the SoftEther VPN Client service ...
SoftEther VPN Client service has been stopped.

Regards

Barry Margolin

unread,
Apr 2, 2015, 8:15:07 PM4/2/15
to
In article <mfkl2q$ieg$2...@aspen.stu.neva.ru>,
Hongyi Zhao <hongy...@gmail.com> wrote:

> On Thu, 02 Apr 2015 19:32:49 -0400, Barry Margolin wrote:
>
> > Why do you think "wait" will help? Can't you tell from the original
> > problem that "vpnclient start" doesn't wait for the VPN to be ready
> > before it returns?
>
> Sorry for my misunderstanding the notes.
>
> You said that the `The purpose of "wait" is to wait for background jobs
> to finish.'
>
> So I put the running of "vpnclient start" into background, and then wait
> for its finished status, and then execute the subsequent commands.
>
> But this still cann't ensure the `vpnclient servive' has been ready for
> use.

Think about your original script, which just had the commands running
normally. When you run commands normally, each one runs when the
previous one finishes. But the problem you had was that even though the
command had completed, the VPN wasn't actually ready to use -- that's
why you needed to sleep.

Putting the command into the background and waiting for it to finish
isn't going to change the fact that the VPN isn't ready to use when the
command finishes. Presumably it has merely initiated something in the
VPN driver, and returns immediately without waiting for everything to
complete.

>
>
> >
> > Like I said in my previous reply, you have to test something else that
> > gets changed on the system when the VPN is ready to use.
>
>
> Ok, I've find that after the vpnclient start are ready, it will give the
> following messages on the stdout:
>
>
> werner@debian:~$ sudo vpnclient start
> The SoftEther VPN Client service has been started.
>
> or for the Client service has been already started:
>
> werner@debian:~$ sudo vpnclient start
> SoftEther VPN Client service has been already started.
> Run the "vpnclient stop" command to stop this service.
>
> So I use the following script to test:
>
> --------
> rm -f aaa
> sudo vpnclient start | egrep '(started|already)' > aaa
> if [[ -s aaa ]]
> then
> sudo vpncmd /client localhost /CMD niccreate nic
> sudo vpncmd /client localhost /CMD nicdelete nic
> sudo vpnclient stop
> fi
> --------
>
> But still failed, see the following for detail:

It's STILL the case that when the command is done, the VPN isn't ready
to use. You have to go into a loop, checking something that will be
changed in the system when it's ready. The fact that it prints a message
doesn't change that.

Hongyi Zhao

unread,
Apr 2, 2015, 8:48:21 PM4/2/15
to
On Thu, 02 Apr 2015 20:15:04 -0400, Barry Margolin wrote:

> It's STILL the case that when the command is done, the VPN isn't ready
> to use. You have to go into a loop, checking something that will be
> changed in the system when it's ready.

The difficulty for the problem is this. It doesn't give any switches to
look for the things it has been done during its initialization.

So I don't know from where should I inspect for the changes caused by it.

> The fact that it prints a message
> doesn't change that.
>
> bar...@alum.mit.edu

Hongyi Zhao

unread,
Apr 2, 2015, 10:48:02 PM4/2/15
to
On Fri, 03 Apr 2015 00:48:18 +0000, Hongyi Zhao wrote:

> On Thu, 02 Apr 2015 20:15:04 -0400, Barry Margolin wrote:
>
>> It's STILL the case that when the command is done, the VPN isn't ready
>> to use. You have to go into a loop, checking something that will be
>> changed in the system when it's ready.
>
> The difficulty for the problem is this. It doesn't give any switches to
> look for the things it has been done during its initialization.
>
> So I don't know from where should I inspect for the changes caused by
> it.

Got it. Based on you hints and after some tries, I found that the
following command can be used as the probe for inspecting the starting
status of vpnclient service:


sudo vpncmd /client localhost /cmd:exit

If the vpnclient service is available, then the message is as follows:

-----------
werner@debian:~$ sudo vpncmd /client localhost /cmd:exit
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Connected to VPN Client "localhost".

VPN Client>exit
-----------

If not, the message is as follows:


-------------
werner@debian:~$ sudo vpncmd /client localhost /cmd:exit
vpncmd command - SoftEther VPN Command Line Management Utility
SoftEther VPN Command Line Management Utility (vpncmd command)
Version 4.14 Build 9529 (English)
Compiled 2015/02/02 17:33:33 by yagi at pc30
Copyright (c) SoftEther VPN Project. All Rights Reserved.

Error occurred. (Error code: 1)
Connection to the server failed. Check network connection and make sure
that address and port number of destination server are correct.
-------------

Based on the above facts, I revise my scripts into follows and it does
the trick:

----------
sudo vpnclient start
while test -z `sudo vpncmd /client localhost /cmd:exit | egrep
'^Connected'` ;
do
sleep 0.01
done

sudo vpncmd /client localhost /CMD niccreate nic
sudo vpncmd /client localhost /CMD nicdelete nic
sudo vpnclient stop
----------

Regards
0 new messages