1

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.

Endogen
  • 589
  • 2
  • 12
  • 24
  • I might have misanswered, by "not return and the terminal will be blocked" do you mean that you'd rather not have a terminal window hanging around while the program is running, i.e. it should run silently? I think some other workarounds for that involve either using AppleScript or Automator, or bundling it with Platypus. – chrstphrchvz Apr 03 '17 at 21:20
  • To be honest, that's exactly what i cant to do: Bundle that script with Platypus! Right now, if i have a script with only `#!/bin/bash caffeinate` then the app will hang - meaning that it is not responsive. That's why i try to get a working script for Platypus. – Endogen Apr 19 '17 at 20:18

2 Answers2

1

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).

Fred
  • 6,590
  • 9
  • 20
  • Sorry for the late reply: Still not what i need. When i execute the script, the terminal is still not ready to execute another command after running the script. When i run this, i can not do anything else in the terminal. When i run this in the same directory as the script with `./caffeinate` (which is the filename of the script) then i get the error `trap: usage: trap [-lp] [arg signal_spec ...]` – Endogen Apr 19 '17 at 20:33
  • Sorry, there was a bug in my code (missing `EXIT` at the end of the `trap` line). However, remember this script must be sourced (`. ./caffeinate` or `source caffeinate`), not executed in a separate process like you were doing. – Fred Apr 19 '17 at 21:10
  • Does what it should do. Thanks for that! Another question: Is there a way to add 'source' or './' into the script? Because i need to run just the script. I don't have any way to add that while executing the script – Endogen Apr 20 '17 at 22:14
  • @Endogen You cannot have the script itself decide it will be sourced. `source` and `.` are shell built-ins. When used, the current shell will read the target script file, and execute its commands inside the current process (as if you typed the commands). When executing a script (or any other program for that matter), the OS will fork a new process, and then start executing commands inside the script, so by the time your script does anything, it is already too late. – Fred Apr 21 '17 at 01:42
  • @Endogen Depending on how you launch your script, there may, however, be a way to source it from a shell startup script that is itself sourced (more info here: https://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files). Bash looks for and sources specific files when starting (please pay attention to the docs, different files are called in different scenarios), and then goes into interactive mode. – Fred Apr 21 '17 at 01:44
0

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.)

Community
  • 1
  • 1
chrstphrchvz
  • 657
  • 5
  • 18