I have a script to automate starting a few servers on the terminal. These are server processes of the software I am developing. They can be recompiled and changed multiple times a day. The binaries can live in different places (working copies).
My script uses
set sudo to do shell script "sudo -v "
to ask for the root password once, then creates a new iterm2 terminal window (and some tabs) and tries to start the servers with sudo:
#!/usr/bin/osascript
on run args
set sudo to do shell script "sudo -v " -- to ask for password once only
tell application "iTerm"
create window with default profile
tell current window
tell current session
write text "sudo /some/server"
end tell
-- more tabs and more sudos go here
end tell
end tell
end run
Everything worked fine on El Capitan, but not on High Sierra.
If the sudo
password is not cached, the sudo -v
asks for my password and then proceeds --- as expected. If I ran sudo
before and the password is cached, the sudo -v
does not ask for a password, again, as expected.
But in both cases each of the subsequent sudo
s in the new window does ask for my password, making the whole thing inconvenient.
When I add with administrator privileges
to the initial sudo
it does ask for my password in a popup dialog but the following sudo
calls with write text
still ask for the password on the shell.
(I use write text
and the terminal to keep an eye on these servers' outputs.)
I can run regular sudo commands just fine and it does cache the password.
How can I use sudo
in Applescript in a new iTerm2 window and have it ask for the password only once?
EDIT: Looks like this is not related to Applescript but to iterm and High Sierra. When I open a terminal window with 2 tabs, I need to enter my password in both when I run e.g., sudo ls
. After that both tabs have the sudo password cached, but running sudo in one tab does not make the other tab use the cached sudo password.
So the real question is probably: how do I get iTerm2 to cache the sudo password and share that between tabs and windows?
sudoers
file? Have a look at the manual page forsudoers
.sudoers
file before usingpassword
withadministrator privileges
as the password is saved in plain text within the AppleScript file.sudo <script_name>, I'm prompted for my password and then the script runs all of the
write text "sudo /some/server"` commands without prompting again for a password. In macOS 10.13, all of thewrite text "sudo /some/server"
commands wait for me to input the password again.sudo
will not cache credentials in separate shells" and that is not totally true, case in point! I can reproduce the OP's issue! In OS X 10.8.6 and OS X 10.11.6 his script works as wanted and intended however, is does not work in macOS 10.13.1 as it did in OS X 10.8.6 and OS X 10.11.6. So, something changed between OS X 10.11.6 and macOS 10.13.1 that I tested under. So while "sudo
will not cache credentials in separate shells." is true in macOS 10.13.1 it is not true in OS X 10.8.6 and OS X 10.11.6 as tested. I'll admit I didn't think it would work but it did!