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

"rewriting" bash pipeline

0 views
Skip to first unread message

rihad

unread,
Nov 25, 2003, 4:13:12 PM11/25/03
to
Suppose I'm executing a lengthy command, `make universe', and some
time later decide I want the machine to power off when that
command is done. So I hit ^Z, and say
# fg %1 && poweroff
Oddly, it does what I expect ("oddly" because I came up with it myself).
So far, so good. But what if a moment later I decide NOT to power off? Is
there something clever to be done to undo the addition of the
" && poweroff" part?

Alan Connor

unread,
Nov 25, 2003, 4:58:59 PM11/25/03
to

I wouldn't do that. You are asking to have your filesystem trashed.

Try adding a line like:

read nn; echo "do you want to poweroff? If so, enter \"yes\" now: " \

if test "$nn" = "yes" ; then poweroff; fi

You can put that in a script in your PATH and call the script by name,
such as "po". Or as a function in your .bashrc.


AC

Alan Connor

unread,
Nov 25, 2003, 5:03:11 PM11/25/03
to

Oops! Put the "read nn" AFTER the echo part and before the "if test..."

AC

rihad

unread,
Nov 25, 2003, 5:57:18 PM11/25/03
to
On Tue, 25 Nov 2003 21:58:59 +0000, Alan Connor wrote:

> On Wed, 26 Nov 2003 01:13:12 +0400, rihad <ri...@mail.ru> wrote:
>>
>>
>> Suppose I'm executing a lengthy command, `make universe', and some
>> time later decide I want the machine to power off when that
>> command is done. So I hit ^Z, and say
>> # fg %1 && poweroff
>> Oddly, it does what I expect ("oddly" because I came up with it myself).
>> So far, so good. But what if a moment later I decide NOT to power off? Is
>> there something clever to be done to undo the addition of the
>> " && poweroff" part?
>
> I wouldn't do that. You are asking to have your filesystem trashed.

That's confusing... you mean poweroff does an immediate power off without
unmounting filesystems, syncing to disk etc.? poweroff is just a symlink
to halt, and halt tells init to switch to runlevel 0 or 6 *first* if it's
not already there. See halt(8).

>
> Try adding a line like:
>
> read nn; echo "do you want to poweroff? If so, enter \"yes\" now: " \
>
> if test "$nn" = "yes" ; then poweroff; fi

The whole point was for those commands to run unattended. Like a
lengthy download with wget with the "shutdown when done" checkbox checked :)

Chris F.A. Johnson

unread,
Nov 25, 2003, 6:05:51 PM11/25/03
to
On Tue, 25 Nov 2003 at 21:13 GMT, rihad wrote:
> Suppose I'm executing a lengthy command, `make universe', and some
> time later decide I want the machine to power off when that
> command is done. So I hit ^Z, and say
> # fg %1 && poweroff

What are you doing that requires root?

> Oddly, it does what I expect ("oddly" because I came up with it myself).
> So far, so good. But what if a moment later I decide NOT to power off? Is
> there something clever to be done to undo the addition of the
> " && poweroff" part?

Press ^C (Control-C).

But you'd be better off adding a mechanism to do it cleanly. One
possibility is:

fg %1 && { read -sn1 -t1 || poweroff; }

If you press a key before the job is finished, poweroff will not be
executed.

Better would be:

fg %1 && { read -sn1 -t1 || sudo poweroff; }

...and run the job as a regular user unless absolutely necessary.

--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Leon.

unread,
Nov 25, 2003, 7:47:41 PM11/25/03
to

"rihad" <ri...@mail.ru> wrote in message
news:pan.2003.11.25....@mail.ru...


If you kill the shell, "make universe" would be killed too.
If you nohup "make universe", the shell will run poweroff immeadiately
instead of waiting for "make universe" to finish.

The command is stored inside the shell's memory, the shell doesnt provide
for this command to be modified "on the fly", although its possible to use
debugging tools to go in and change the variable that holds the command -
being careful to change the correct variable to correct values, of course.


But in normal world, you should use a supervisory script, run from cron
or some other way that makes it run periodically.

if "make universe" is still exists, it just exits
if make universe has finished, then poweroff is called..


if you dont want it to poweroff, you can take the script out of cron...

Ed Murphy

unread,
Nov 26, 2003, 3:13:32 AM11/26/03
to

Create a script that follows the following pseudo-logic:

if <file> exists then poweroff

Do 'fg %1 && <script>'.

Create <file>.

If you change your mind, then remove <file>.

rihad

unread,
Nov 26, 2003, 4:59:05 AM11/26/03
to

That's the kind of solution I was looking for, excellent abstraction,
thanks! :)

$ make universe && [ -f file ] && sudo poweroff
$ ^Z
$ touch file
$ fg %1
...
$ ^Z
$ rm file
$ fg %1
...

rihad

unread,
Nov 26, 2003, 5:03:24 AM11/26/03
to
On Wed, 26 Nov 2003 08:13:32 +0000, Ed Murphy wrote:

That's the kind of solution I was looking for, excellent abstraction,
thanks! :)

$ ( make universe && [ -f file ] && sudo poweroff )

William Park

unread,
Nov 26, 2003, 4:35:50 PM11/26/03
to
rihad <ri...@mail.ru> wrote:
> > Create a script that follows the following pseudo-logic:
> >
> > if <file> exists then poweroff
> >
> > Do 'fg %1 && <script>'.
> >
> > Create <file>.
> >
> > If you change your mind, then remove <file>.
>
> That's the kind of solution I was looking for, excellent abstraction,
> thanks! :)
>
> $ make universe && [ -f file ] && sudo poweroff
> $ ^Z
> $ touch file
> $ fg %1
> ...
> $ ^Z
> $ rm file
> $ fg %1

Or, put 'poweroff' in another process, ie.
make universe &
while ps -C make; do sleep 1; done && poweroff &

--
William Park, Open Geometry Consulting, <openge...@yahoo.ca>
Linux solution for data management and processing.

LEE Sau Dan

unread,
Nov 26, 2003, 4:37:52 PM11/26/03
to
>>>>> "rihad" == rihad <ri...@mail.ru> writes:

rihad> $ make universe && [ -f file ] && sudo poweroff
rihad> $ ^Z

Why don't you avoid the ^Z by simply:

$ make universe && [ -f file ] && sudo poweroff &

??

--
Lee Sau Dan 李守敦(Big5) ~{@nJX6X~}(HZ)

E-mail: dan...@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee

LEE Sau Dan

unread,
Nov 26, 2003, 4:36:30 PM11/26/03
to
>>>>> "Leon" == Leon <le...@noteon.net> writes:

Leon> If you kill the shell, "make universe" would be killed too.

This is not the case, according to my 10 years of experiences with
interactive bash. (I can't recall whether C shell would kill the
background jobs when it is killed. I've switched completely to bash
for so long.) This is because 'huponexit' is not set in my working
environment. What you said is only try if you try to send a SIGHUP to
the shell, or you have set 'huponexit'. 'man bash' for details.

LEE Sau Dan

unread,
Nov 26, 2003, 4:30:24 PM11/26/03
to
>>>>> "rihad" == rihad <ri...@mail.ru> writes:

rihad> Suppose I'm executing a lengthy command, `make universe',
rihad> and some time later decide I want the machine to power off
rihad> when that command is done. So I hit ^Z, and say # fg %1 &&
rihad> poweroff Oddly, it does what I expect ("oddly" because I
rihad> came up with it myself). So far, so good. But what if a
rihad> moment later I decide NOT to power off? Is there something
rihad> clever to be done to undo the addition of the " &&
rihad> poweroff" part?

Instead of 'poweroff', run a script called 'conditionally_poweroff'
which could be something like the following *untested* code:

#!/bin/sh
inhitbit_file=/etc/dont_poweroff
/bin/test -f $inhibit_file || exec /sbin/poweroff
echo "NOT powering off because $inhibit_file is present"

When you want to change your mind, do a "touch /etc/dont_poweroff".
If you change your mind once again, do a "rm /etc/dont_poweroff".


The idea is not anything new. Read 'man login'. I think this trick
dates back to the very first days of unix.

LEE Sau Dan

unread,
Nov 26, 2003, 4:24:36 PM11/26/03
to
>>>>> "Alan" == Alan Connor <zzz...@xxx.yyy> writes:

>> Suppose I'm executing a lengthy command, `make universe', and
>> some time later decide I want the machine to power off when
>> that command is done. So I hit ^Z, and say # fg %1 && poweroff
>> Oddly, it does what I expect ("oddly" because I came up with it
>> myself). So far, so good. But what if a moment later I decide
>> NOT to power off? Is there something clever to be done to undo
>> the addition of the " && poweroff" part?

Alan> I wouldn't do that. You are asking to have your filesystem
Alan> trashed.

Please explain why you think so.

John Thompson

unread,
Nov 27, 2003, 9:13:35 PM11/27/03
to
On 2003-11-25, rihad <ri...@mail.ru> wrote:

> The whole point was for those commands to run unattended. Like a
> lengthy download with wget with the "shutdown when done" checkbox checked :)

Wget has a checkbox?

--

-John (JohnTh...@new.rr.com)

Alan Connor

unread,
Nov 27, 2003, 9:58:58 PM11/27/03
to
On Fri, 28 Nov 2003 02:13:35 GMT, John Thompson <jo...@starfleet.os2.dhs.org> wrote:
>
>
> On 2003-11-25, rihad <ri...@mail.ru> wrote:
>
>> The whole point was for those commands to run unattended. Like a
>> lengthy download with wget with the "shutdown when done" checkbox checked :)
>
> Wget has a checkbox?
>
>

No. That's not what he means. I'm following him now, and it would be an easy
script to write.

But since he didn't bother to thank me for the first one, and just comes across
as an uncommunicative smartass, he can take a hike.

AC

rihad

unread,
Nov 28, 2003, 2:57:07 AM11/28/03
to

Sorry if you get that impression. I really appreciate your trying to be
helpful!

Alan Connor

unread,
Nov 28, 2003, 3:49:39 AM11/28/03
to

Okay, then I apologize too.

But I'm still not clear on what you want, not completely.

#!/bin/sh

wget -P /boot/dor -p -i /home/rihad/.WGET/special -o /boot/dor/log -t\
3 -nc

if test ! -f /home/rihad/nohalt ; then

halt

else rm /home/rihad/nohalt && exit 0

fi

So, with this solution, all you have to do is create a file called nohalt
in your home directory and there'll be no halt after wget is through.

Getting close, rihad?.

AC

0 new messages