I wrote a little bash script showing a screenshot menu (screenshot-menu.bash
):
#!/usr/bin/env bash
echo 'Select an option:
s whole screen
w active window
r select region
'
read -rsn1 key
sleep 0.1
case $key in
s|w|r) nohup ./screenshot.bash -$key & ;;
*) echo Canceled. >> ./log.txt ;;
esac
This script runs nohup ./screenshot.bash
with a parameter. This is screenshot.bash
:
#!/usr/bin/env bash
while [[ "$#" > 0 ]]; do case $1 in
-s|--screen) PARAM=''; TYPE='Screen'; shift;;
-w|--window) PARAM='-u'; TYPE='Window'; shift;;
-r|--region) PARAM='-s'; TYPE='Region'; shift;;
*) echo "Unknown parameter passed: $1"; exit 1; shift; shift;;
esac; done
sleep 0.2
scrot $PARAM $(xdg-user-dir PICTURES)/'%Y-%m-%d %H.%M.%S $wx$h '$TYPE.png -e 'echo "$f" && notify-send -u low "$f"'
When I run ./screenshot-menu.bash
inside a terminal, then enter r
, everything works fine: I can select a region and I get a screenshot of that region.
But I want to be able to call this script from everywhere. That's why I have a keybinding that runs this script in a new terminal window:
alacritty -t "Screenshot menu" -o "window.dimensions.columns=20" -o "window.dimensions.lines=7" -e ~/bin/screenshot-menu.bash
(Or simpler gnome-terminal -- ~/bin/screenshot.bash
.)
That's the reason I use nohup
. I want the terminal to exit because I don't want to have it in the screenshot. But of course, screenshot.bash
must not die.
The second method (script in a new terminal window) works maybe once in five times. The other times, the cursor doesn't turn into a cross-hair for selecting a region. I suppose, screenshot.bash
dies although I use nohup
.
What could be causing this random behavior?
screenshot-menu.bash
gets aSIGHUP
and exits beforenohup ./screenshot.bash
can actually run. You may be able to confirm this by adding a shortsleep
at the end ofscreenshot-menu.bash
. I'm not saying this would be a good solution, though; there likely is a better approach. – fra-san Apr 02 '21 at 15:17screenshot-menu.bash
" in place of "the shell that runsscreenshot-menu.bash
". – fra-san Apr 02 '21 at 15:26sleep
calls? – Eduardo Trápani Apr 02 '21 at 15:55sleep 0.1
at the end seems to fix it! I tried it five times and it worked every time. Thank a lot! – MaxGyver Apr 02 '21 at 18:36r
) cancelsscrot
immediately. I added a second sleep when I split this script into two because I wanted to make sure, the terminal closes before the screenshot is taken. But onesleep
only inscreenshot.bash
should be sufficient. – MaxGyver Apr 02 '21 at 18:38