You are not logged in.

#1 2011-10-26 12:35:46

awayand
Member
Registered: 2009-09-25
Posts: 398

[SOLVED] killing a background job inside a loop

I have the following script running in the background:

while true; do
	pkill trayer
        ~/bin/ttrayer &
	sleep 60
done &

Now I have changed my mind and want to kill it. How can I do this? If I execute "pkill sleep", then it will loop back into the beginning of the while loop. How can I get out of there?

Last edited by awayand (2011-10-27 02:58:17)

Offline

#2 2011-10-26 13:11:49

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: [SOLVED] killing a background job inside a loop

$ fg
.......
<Ctrl-C>

$

Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#3 2011-10-26 14:02:26

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] killing a background job inside a loop

problem is the script is inside a script file that launches dwm, so I would have to kill dwm as well which I don't really want to. sorry, a small detail I should have mentioned...

Offline

#4 2011-10-26 17:12:24

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: [SOLVED] killing a background job inside a loop

You can add this after the while loop:

echo $! >/tmp/trayer_refresh.pid

And then to kill it:

kill `cat /tmp/trayer_refresh.pid`

Offline

#5 2011-10-26 22:52:03

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Re: [SOLVED] killing a background job inside a loop

You could put a trap right after the backgrounded loop, e.g.

trap "kill $! && echo 'Caught SIGINT: Break'; exit 1" SIGINT SIGKILL

where $! would contain the PID of the previously run process, e.g. the backgrounded loop.

Then pressing ^C will break out of the loop.

Last edited by egan (2011-10-26 22:52:36)

Offline

#6 2011-10-26 23:15:11

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] killing a background job inside a loop

@stqn: didn't work I am showing PIDs that are no longer active, both files containing the same PID:

while true; do
echo $! >/tmp/trayer_after_do.pid
	pkill trayer 
        ~/bin/ttrayer &
	sleep 60
echo $! >/tmp/trayer_before_done.pid
done &

@egan: nice solution, but won't work, as I can't ^C out of the script as it is part of my window manager startup script.

To get the whole picture:

.xinitrc starts my script ~/bin/dwmst which contains:

while true; do
	pkill trayer
	~/bin/ttrayer &
	sleep 60
done &

while true; do
	dwm || exit
done

Offline

#7 2011-10-26 23:22:16

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: [SOLVED] killing a background job inside a loop

"After the while loop" means right after "done &" smile.

Offline

#8 2011-10-26 23:48:00

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] killing a background job inside a loop

@stqn: I see. Now it worked perfectly. You are nice, I like you.

Is there a more elegant solution? I just tried this, analogous to my dwm loop, and I could break out of the loop by doing "pkill sleep".

while true; do
	pkill trayer
	~/bin/ttrayer &
	sleep 60 || exit
done &

Would there be an even more elegant solution, or is this the way to go?

Offline

#9 2011-10-27 13:25:53

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: [SOLVED] killing a background job inside a loop

If you have several scripts running sleep then pkill will kill them all.

I don't have a better idea...

Last edited by stqn (2011-10-27 13:26:42)

Offline

#10 2011-10-27 14:00:54

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] killing a background job inside a loop

thanks! smile

Offline

Board footer

Powered by FluxBB