Reputation: 651
I would like to execute a bash script on my Mac that will start the command line program caffeinate
. But if i start it like this:
#!/bin/bash
caffeinate
It will not return and the terminal will be blocked. That's not what i want. But on the other hand, if i close the terminal the program should also exit. How can i achieve that?
I already tried to play around with return
and exit
but couldn't get what i wanted.
Upvotes: 1
Views: 1530
Reputation: 6995
Try something like :
#!/bin/bash
trap 'kill -TERM $caffeinate_pid' EXIT
caffeinate &
caffeinate_pid=$!
This script will need to be sourced (with source
or .
) so that the trap is in the context of the calling shell (the termination of which you want to use to trigger termination of caffeinate).
Upvotes: 1
Reputation: 727
This is caused by Terminal.app, where the existing workarounds are to go into Terminal's preferences and set "When the shell exits:" to "Close window", and either setting "Ask before closing:" to "Never" or "Only if there are other processes other then the login shell and:" and adding your program to the list of programs that don't prompt the user when closing the window.(Not sure the second part works for scripts, nor whether there is a more universal workaround using more bash and AppleScript code so that an end user need not change any preferences.)
Upvotes: 0