public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* How to repeat a bash shell script until success
@ 2017-07-14  0:16 Bryan Dunphy
  2017-07-14  1:12 ` Brian Inglis
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Bryan Dunphy @ 2017-07-14  0:16 UTC (permalink / raw)
  To: cygwin

I have a shell script, originally created for Mac OS X. that waits for an external drive to be mounted (by testing an “ls” of the volume’s root directory for success) then runs an “rsync”
 command. How do I get the script to be run repeatedly until successful exit under Cygwin?

Here is the unmodified Mac OS version of the script:

#!/bin/bash
if ls /Volumes/Shared >/dev/null 2>/dev/null
then
        rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
        exit 0
else
        exit 1
fi


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14  0:16 How to repeat a bash shell script until success Bryan Dunphy
@ 2017-07-14  1:12 ` Brian Inglis
  2017-07-14  5:31 ` Gary Johnson
  2017-07-14 17:34 ` Kaz Kylheku
  2 siblings, 0 replies; 11+ messages in thread
From: Brian Inglis @ 2017-07-14  1:12 UTC (permalink / raw)
  To: cygwin

On 2017-07-12 09:35, Bryan Dunphy wrote:
> I have a shell script, originally created for Mac OS X. that waits for an 
> external drive to be mounted (by testing an “ls” of the volume’s root 
> directory for success) then runs an “rsync” command. How do I get the script
> to be run repeatedly until successful exit under Cygwin?
> 
> Here is the unmodified Mac OS version of the script:
> 
> #!/bin/bash
> if ls /Volumes/Shared >/dev/null 2>/dev/null
> then
>     rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' \
>	aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
>         exit 0
> else
>         exit 1
> fi

Plug your subject line into a web search and read the resulting hits.
Then you don't have to wait 9 hours for hints. ;^>
You could also run "man bash" and RTFM.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14  0:16 How to repeat a bash shell script until success Bryan Dunphy
  2017-07-14  1:12 ` Brian Inglis
@ 2017-07-14  5:31 ` Gary Johnson
  2017-07-14 13:06   ` Gary Johnson
  2017-07-14 17:07   ` cyg Simple
  2017-07-14 17:34 ` Kaz Kylheku
  2 siblings, 2 replies; 11+ messages in thread
From: Gary Johnson @ 2017-07-14  5:31 UTC (permalink / raw)
  To: cygwin

On 2017-07-12, Bryan Dunphy wrote:
> I have a shell script, originally created for Mac OS X. that waits
> for an external drive to be mounted (by testing an “ls” of the
> volume’s root directory for success) then runs an “rsync” command.
> How do I get the script to be run repeatedly until successful exit
> under Cygwin?
> 
> Here is the unmodified Mac OS version of the script:
> 
> #!/bin/bash
> if ls /Volumes/Shared >/dev/null 2>/dev/null
> then
>         rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
>         exit 0
> else
>         exit 1
> fi

Let the name of your script be "myscript".  The following will run
myscript every two seconds until it succeeds.

    while ! myscript; do sleep 2; done

This is really a bash programming question and is not specific to
Cygwin.

Regards,
Gary


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14  5:31 ` Gary Johnson
@ 2017-07-14 13:06   ` Gary Johnson
  2017-07-14 17:07   ` cyg Simple
  1 sibling, 0 replies; 11+ messages in thread
From: Gary Johnson @ 2017-07-14 13:06 UTC (permalink / raw)
  To: cygwin

On 2017-07-12, Bryan Dunphy wrote:
> I have a shell script, originally created for Mac OS X. that waits
> for an external drive to be mounted (by testing an “ls” of the
> volume’s root directory for success) then runs an “rsync” command.
> How do I get the script to be run repeatedly until successful exit
> under Cygwin?
> 
> Here is the unmodified Mac OS version of the script:
> 
> #!/bin/bash
> if ls /Volumes/Shared >/dev/null 2>/dev/null
> then
>         rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
>         exit 0
> else
>         exit 1
> fi

Let the name of your script be "myscript".  The following will run
myscript every two seconds until it succeeds.

    while ! myscript; do sleep 2; done

This is really a bash programming question and is not specific to
Cygwin.

Regards,
Gary


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14  5:31 ` Gary Johnson
  2017-07-14 13:06   ` Gary Johnson
@ 2017-07-14 17:07   ` cyg Simple
  2017-07-15 18:40     ` Sorin Adrian Savu
  2017-07-17 17:46     ` Ian Lambert via cygwin
  1 sibling, 2 replies; 11+ messages in thread
From: cyg Simple @ 2017-07-14 17:07 UTC (permalink / raw)
  To: cygwin

On 7/14/2017 1:27 AM, Gary Johnson wrote:
> On 2017-07-12, Bryan Dunphy wrote:
>> I have a shell script, originally created for Mac OS X. that waits
>> for an external drive to be mounted (by testing an “ls” of the
>> volume’s root directory for success) then runs an “rsync” command.
>> How do I get the script to be run repeatedly until successful exit
>> under Cygwin?
>>
>> Here is the unmodified Mac OS version of the script:
>>
>> #!/bin/bash
>> if ls /Volumes/Shared >/dev/null 2>/dev/null
>> then
>>         rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
>>         exit 0
>> else
>>         exit 1
>> fi
> 
> Let the name of your script be "myscript".  The following will run
> myscript every two seconds until it succeeds.
> 
>     while ! myscript; do sleep 2; done
> 
> This is really a bash programming question and is not specific to
> Cygwin.
> 

In reality the OP script appears to be executed in a crontab system and
executed every X minutes.  So a change to the OP question is needed
which is answered at[1].  Let's remember some people have no real clue
as to what question they should ask and we need to interpret what is
being asked into what should have been asked.  If interpretation isn't
possible then asking for a use case would be warranted.

[1]
https://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows

-- 
cyg Simple

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14  0:16 How to repeat a bash shell script until success Bryan Dunphy
  2017-07-14  1:12 ` Brian Inglis
  2017-07-14  5:31 ` Gary Johnson
@ 2017-07-14 17:34 ` Kaz Kylheku
  2017-07-14 19:20   ` Brian Inglis
  2 siblings, 1 reply; 11+ messages in thread
From: Kaz Kylheku @ 2017-07-14 17:34 UTC (permalink / raw)
  To: cygwin

On 12.07.2017 08:35, Bryan Dunphy wrote:
> I have a shell script, originally created for Mac OS X. that waits for
> an external drive to be mounted (by testing an “ls” of the volume’s
> root directory for success) then runs an “rsync”
>  command. How do I get the script to be run repeatedly until
> successful exit under Cygwin?

Multi-line

   while ! program arg1 arg2 ...
   do
     :   # explicit null statement: syntactically mandatory!
   done

one-liner: semicolon between program and "do",
semicolon between : statement and "done":

   while ! program arg1 arg2 ... ; do : ; done

The space between ! and the command is required.

I would throw a sleep in there, not to create a CPU-intensive
busy loop:

   while ! program args ... ; do
     sleep 1
   done


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14 17:34 ` Kaz Kylheku
@ 2017-07-14 19:20   ` Brian Inglis
  0 siblings, 0 replies; 11+ messages in thread
From: Brian Inglis @ 2017-07-14 19:20 UTC (permalink / raw)
  To: cygwin

On 2017-07-14 11:07, Kaz Kylheku wrote:
> On 12.07.2017 08:35, Bryan Dunphy wrote:
>> I have a shell script, originally created for Mac OS X. that waits for
>> an external drive to be mounted (by testing an “ls” of the volume’s
>> root directory for success) then runs an “rsync”
>>  command. How do I get the script to be run repeatedly until
>> successful exit under Cygwin?
> Multi-line
>   while ! program arg1 arg2 ...
>   do
>     :   # explicit null statement: syntactically mandatory!
>   done
> one-liner: semicolon between program and "do",
> semicolon between : statement and "done":
>   while ! program arg1 arg2 ... ; do : ; done
> The space between ! and the command is required.
> I would throw a sleep in there, not to create a CPU-intensive
> busy loop:
>   while ! program args ... ; do
>     sleep 1
>   done

Has everyone forgotten there is also an until loop, supported by at least sh,
dash, bash, [m]ksh:
	until prog ...; do sleep 60; done

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14 17:07   ` cyg Simple
@ 2017-07-15 18:40     ` Sorin Adrian Savu
  2017-07-17 17:41       ` cyg Simple
  2017-07-17 17:46     ` Ian Lambert via cygwin
  1 sibling, 1 reply; 11+ messages in thread
From: Sorin Adrian Savu @ 2017-07-15 18:40 UTC (permalink / raw)
  To: cygwin

On Fri, Jul 14, 2017 at 4:06 PM, cyg Simple <cygsimple@gmail.com> wrote:
>
> On 7/14/2017 1:27 AM, Gary Johnson wrote:
> > On 2017-07-12, Bryan Dunphy wrote:
> >> I have a shell script, originally created for Mac OS X. that waits
> >> for an external drive to be mounted (by testing an “ls” of the
> >> volume’s root directory for success) then runs an “rsync” command.
> >> How do I get the script to be run repeatedly until successful exit
> >> under Cygwin?
> >>
> >> Here is the unmodified Mac OS version of the script:
> >>
> >> #!/bin/bash
> >> if ls /Volumes/Shared >/dev/null 2>/dev/null
> >> then
> >>         rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
> >>         exit 0
> >> else
> >>         exit 1
> >> fi
> >
> > Let the name of your script be "myscript".  The following will run
> > myscript every two seconds until it succeeds.
> >
> >     while ! myscript; do sleep 2; done
> >
> > This is really a bash programming question and is not specific to
> > Cygwin.
> >
>
> In reality the OP script appears to be executed in a crontab system and
> executed every X minutes.  So a change to the OP question is needed
> which is answered at[1].  Let's remember some people have no real clue
> as to what question they should ask and we need to interpret what is
> being asked into what should have been asked.  If interpretation isn't
> possible then asking for a use case would be warranted.
>
> [1]
> https://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows


Actually, the correct question would be: how do I run a bash script
when a USB stick is mounted ?

Which is doable via Task Scheduler and a event log trigger. See
https://answers.microsoft.com/en-us/windows/forum/windows_vista-windows_programs/task-scheduler-how-to-automatically-synchronize-my/45a49d83-b1d8-4d37-8896-3d2696cf9795
on how to locate the appropriate event,

As for the script:

- don't use ls for checking file/directory presence use -f/-d (man test)
- you will need to adjust paths (cygwin has windows drives under
/cygdrive (cygpath -h)

On how to run the script from Task Scheduler use an action like this:

C:\Tools\cygwin64\bin\bash.exe -c /cygdrive/c/tools/cygwin64/home/bryan/bkp.sh
(adjust your paths accordingly )
>
>
>
> --
> cyg Simple
>
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-15 18:40     ` Sorin Adrian Savu
@ 2017-07-17 17:41       ` cyg Simple
  0 siblings, 0 replies; 11+ messages in thread
From: cyg Simple @ 2017-07-17 17:41 UTC (permalink / raw)
  To: cygwin

On 7/15/2017 2:56 AM, Sorin Adrian Savu wrote:
> On Fri, Jul 14, 2017 at 4:06 PM, cyg Simple <cygsimple@gmail.com> wrote:
>>
>> On 7/14/2017 1:27 AM, Gary Johnson wrote:
>>> On 2017-07-12, Bryan Dunphy wrote:
>>>> I have a shell script, originally created for Mac OS X. that waits
>>>> for an external drive to be mounted (by testing an “ls” of the
>>>> volume’s root directory for success) then runs an “rsync” command.
>>>> How do I get the script to be run repeatedly until successful exit
>>>> under Cygwin?
>>>>
>>>> Here is the unmodified Mac OS version of the script:
>>>>
>>>> #!/bin/bash
>>>> if ls /Volumes/Shared >/dev/null 2>/dev/null
>>>> then
>>>>         rsync -avz --compress-level=9 --delete-during --partial --exclude 'cache/' aleph.gutenberg.org::gutenberg /Volumes/Shared/Project-Gutenberg
>>>>         exit 0
>>>> else
>>>>         exit 1
>>>> fi
>>>
>>> Let the name of your script be "myscript".  The following will run
>>> myscript every two seconds until it succeeds.
>>>
>>>     while ! myscript; do sleep 2; done
>>>
>>> This is really a bash programming question and is not specific to
>>> Cygwin.
>>>
>>
>> In reality the OP script appears to be executed in a crontab system and
>> executed every X minutes.  So a change to the OP question is needed
>> which is answered at[1].  Let's remember some people have no real clue
>> as to what question they should ask and we need to interpret what is
>> being asked into what should have been asked.  If interpretation isn't
>> possible then asking for a use case would be warranted.
>>
>> [1]
>> https://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows
> 
> 
> Actually, the correct question would be: how do I run a bash script
> when a USB stick is mounted ?
> 
> Which is doable via Task Scheduler and a event log trigger. See
> https://answers.microsoft.com/en-us/windows/forum/windows_vista-windows_programs/task-scheduler-how-to-automatically-synchronize-my/45a49d83-b1d8-4d37-8896-3d2696cf9795
> on how to locate the appropriate event,
> 
> As for the script:
> 
> - don't use ls for checking file/directory presence use -f/-d (man test)
> - you will need to adjust paths (cygwin has windows drives under
> /cygdrive (cygpath -h)
> 

And don't forget the user has the option to change /cygdrive to
something else including / which is my preference.

> On how to run the script from Task Scheduler use an action like this:
> 
> C:\Tools\cygwin64\bin\bash.exe -c /cygdrive/c/tools/cygwin64/home/bryan/bkp.sh
> (adjust your paths accordingly )

Yes, if you don't want to run cygserver, this is another option.  But if
you're moving from a *nix based system then using cron under cygserver
is what you want.

-- 
cyg Simple

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
  2017-07-14 17:07   ` cyg Simple
  2017-07-15 18:40     ` Sorin Adrian Savu
@ 2017-07-17 17:46     ` Ian Lambert via cygwin
  1 sibling, 0 replies; 11+ messages in thread
From: Ian Lambert via cygwin @ 2017-07-17 17:46 UTC (permalink / raw)
  To: cygwin, cyg Simple

On July 14, 2017 9:06:02 AM EDT, cyg Simple <> wrote:
>On 7/14/2017 1:27 AM, Gary Johnson wrote:
>> On 2017-07-12, Bryan Dunphy wrote:
>>> I have a shell script, originally created for Mac OS X. that waits
>>> for an external drive to be mounted (by .
>> 
>
>In reality the OP script appears to be executed in a crontab system and
>executed every X minutes.  So a change to the OP question is needed
>which is answered at[1].  Let's remember some people have no real clue
>as to what question they should ask and we need to interpret what is
>being asked into what should have been asked.  If interpretation isn't
>possible then asking for a use case would be warranted.
>
>[1]
>https://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows

cyg Simple,
Thanks much for this. It seems the link is misleading, because it says cygrunsrv and cron-config must be used to get Cron to work. 

I've been frustrated without admin, being unable to use these. It turns out simply adding 
/usr/sbin/cron to .bashrc will make Cron available. Or, slightly more complicated, 
to avoid "lock" errors:
$HOME/bin/startcron

Containing:

#!/bin/bash
if P=$(pgrep cron)
then
echo "already running, PID is $P"
else
echo "not running, starting now"
/usr/sbin/cron
fi

Thanks!


-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: How to repeat a bash shell script until success
@ 2017-07-14 19:23 Kaz Kylheku
  0 siblings, 0 replies; 11+ messages in thread
From: Kaz Kylheku @ 2017-07-14 19:23 UTC (permalink / raw)
  To: cygwin

On 14.07.2017 10:34, Brian Inglis wrote:
> On 2017-07-14 11:07, Kaz Kylheku wrote:
>>   while ! program args ... ; do
>>     sleep 1
>>   done
> 
> Has everyone forgotten there is also an until loop, supported by at 
> least sh,
> dash, bash, [m]ksh:

I've forgotten that at least twice. (I don't remember the exact number 
of times.)


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-07-17 17:41 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-14  0:16 How to repeat a bash shell script until success Bryan Dunphy
2017-07-14  1:12 ` Brian Inglis
2017-07-14  5:31 ` Gary Johnson
2017-07-14 13:06   ` Gary Johnson
2017-07-14 17:07   ` cyg Simple
2017-07-15 18:40     ` Sorin Adrian Savu
2017-07-17 17:41       ` cyg Simple
2017-07-17 17:46     ` Ian Lambert via cygwin
2017-07-14 17:34 ` Kaz Kylheku
2017-07-14 19:20   ` Brian Inglis
2017-07-14 19:23 Kaz Kylheku

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).