Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-click shell script issues (.COMMAND on Mac)

Trying to run the following test.command script on Mac with a double-click. (requirement: MUST be run with a double-click)

#!/bin/sh
sudo java -jar ExecutableJar.jar

Here is the output. (The terminal stays open with the below message)

Last login: Mon Aug 13 15:59:05 on ttys001
/Applications/Application\ Folder/test.command ; exit;
code-mac:~ code$ /Applications/Application\ Folder/test.command ; exit;
Unable to access jarfile ExecutableJar.jar
logout

[Process completed]

When I run the same command from Terminal...

sudo java -jar ExecutableJar.jar

...it works fine and opens the executable jar as expected (after prompting for my password). Any ideas? Also, if possible, I'd like the script to either not open a terminal at all, or at the very least close the terminal after starting the executable jar.

Thanks!

like image 635
CODe Avatar asked Dec 16 '22 20:12

CODe


1 Answers

Adding the following to the beginning of the script made it work as expected when double-clicked.

cd "$(dirname "$0")"

The entire test.command script is as follows:

#!/bin/sh
cd "$(dirname "$0")"
sudo java -jar ExecutableJar.jar &

Adding the & at the end of the sudo command makes that executable jar run as a background process and allows further commands after the sudo to be processed, in my case, to close the Terminal window. (Otherwise it stays open)

Lastly, adding either of the following to the end of the script will close it once it's done. The first approach closes all Terminal windows and is a bit of an overkill, but it gets the job done.

killall Terminal

The second will prompt the user to close the Terminal window, giving the user the choice.

osascript -e 'tell application "Terminal" to quit'

A final, important note is that neither closing technque would work for my case. Since my script requires a sudo and prompts the user to enter their password, using either route prompted the user to close the Terminal (osascript) or closed the Terminal (killall) before the user had the chance to enter their password. In order to ask for the password first and then run the executable jar, use:

sudo -v

to prompt for the password if needed, then run the executable jar in the background with & and use killall or osascript.

like image 104
CODe Avatar answered Dec 27 '22 03:12

CODe