Bash/xdotool: Commands work, but not in a script
When you source the file with .
, the commands will run just as if you had entered them in the command line. Thus exit
will exit your currently running shell.
To exit the shell from which the script was executed when forking, you need to get the process id of the parent process. You can try running
kill ${PPID}
in the script instead of exit
to kill the parent shell (tip: try just echoing the pid first and check which process it corresponds to so you don't kill your WM or something).
If ${PPID}
doesn't do it for you, you can also try sending the pid as a parameter to the script, but it depends on how and where it's called.
You said you used urxtvd
/urxvtc
. With that combination, this script kills the terminal from which it was started:
#!/bin/sh
echo kill in 3
sleep 3
kill -1 ${PPID}
so you should be able to use kill -1
in this way to kill a single urxvtc
instance.
Note that if you run this by sourcing, then the urxvtd
instance will be $PPID
for the currently running terminal, and all terminals will die. You don't want that.
Related videos on Youtube
Fruckubus Crunt
Updated on September 18, 2022Comments
-
Fruckubus Crunt over 1 year
I am making a bash script for Linux which closes the terminal window if the window loses focus.
On the command line, I was able to do this:
termwin=$(xdotool getactivewindow) while : do if [[ $(xdotool getactivewindow) != $termwin ]] then exit fi done
It works typed manually into a terminal, but, if I put it into a script, instead of closing the window when focus is lost, the script simply stops. No error or anything, just back to a prompt.
I feel like I'm missing something basic.
EDIT
After reading this...: See here
I tried running this as ". test.sh" rather than "./test.sh" and it worked. The link describes the difference in these methods as running the script as a subprocess or as part of the main process, respectively. Can anyone just explain this, and/or modify the script to run successfully with "./" instead of ". ", in the latter creates issues?
-
uzsolt about 12 yearsMy idea is a bit off-topic: do you know about "exotic" window managers? E.g. awesome (awesome.naquadah.org) - you can do this or similar functions easily.
-
Rony about 12 years@uzsolt, you are to? if me, I am mainly on awesome wm quite some years and now and all.
-
uzsolt about 12 yearsYes :) In awesome this is only about 3 lines :) In bash...
-
Rony about 12 years@uzsolt dear, awesome wm has lua, homebrew widgets are candies :), bashlets is another playground :)
-
Fruckubus Crunt about 12 yearsPlease see edit.
-
Daniel Andersson about 12 yearsDoes
./test.sh && exit
work for you? Justexit
orexit 0
when the condition is met in the script, andexit
will be run in the terminal afterwards. -
Fruckubus Crunt about 12 yearsOh bloody hell, that's exactly what I had to do. I was too absorbed in the script itself to consider running it like that.
-
Rony about 12 yearsscripting is somehow like building pieces as according to nix philosophy. xdotool is so useful. btw, hug@uzolt
-
Rony about 12 years@uzsolt 2 lines now ;) cheers.
-
uzsolt about 12 years@Rony if you calculate the parts of
while
it will about five lines. In awesome: you call a signal function to unfocus, callc:kill()
, typeend
and that's all. It's three lines. And you shouldn't run a plus bash script which eats your CPU :) So awesome is the winner ;)
-
-
Fruckubus Crunt about 12 yearsThis yields the sme results for me, it works if typed in manually, and fails
-
Fruckubus Crunt about 12 yearsI am now wondering if "exit" is the problem. Typed into a terminal, 'exit' closes the terminal, but is it possible that "exit" has a different meaning in a script, that stops the script rather than closing the terminal? Or is this even possible?
-
Fruckubus Crunt about 12 yearsPlease see edit.
-
Fruckubus Crunt about 12 yearsDan, the kill PPID method looks good, but fails with my particular terminal. Im using urxvt in a client/daemon setup (urxvtd and urxvtc). It seems that each terminal in this case has the same ppid, and s =o this method doesn't work. What is different is the pty, as found with the tty command. You can directly tell a pty to do something, right? Something like (not working) "exit >>/dev/pts/0"
-
Daniel Andersson about 12 years
-
Daniel Andersson about 12 years@FruckubusCrunt: Each terminal has the same
PPID
(parent pid), but when you launch a script from a terminal, inside the script the launching terminal's pid will bePPID
, so it sends the signal to the correct pid. However,-1
seems to be needed withurxvtc
. I'll update my answer. -
Rony about 12 years@DanielAndersson thank you very much for your refreshment and very useful link. I use the -9 because my kill does not kill. I shall look into the matter.
-
Fruckubus Crunt about 12 yearsThis is great Dan, thank you. A good learning experience for me. And I certainly could not accept a situation where more terminals would die, as that would defeat the purpose of what I'm doing. So thanks for the solid explanation.