I'm trying to get the Macs at my workplace all updating at a specific time. To do so, I'm running a bash script from a spare Macbook running OS X Server. I have one minor and one big issue with my current script, and could use your help.

The script is very simple, and I don't need it to be too robust for my purposes:

for host in 192.blah.blah.blah 192.blah.blah.blah 192.blah.blah.blah
ssh -t $host sudo softwareupdate -ia
ssh -t $host sudo shutdown -r now

The minor issue: for whatever reason, if I try to put both of those commands on the same line using ; or && like so:

ssh -t $host sudo softwareupdate -ia;sudo shutdown -r now


ssh -t $host sudo softwareupdate -ia && sudo shutdown -r now

it reboots the actual server instead. Not sure where I'm going wrong with the syntax.

The big issue: Despite setting up ssh with a public ssh key (using the instructions I found here), I'm still asked to input a password twice per machine when the script runs. I'm positive it's due to our friend sudo, but the commands don't run at all without it. Ideally, I'll set up a cron job at an early point in the morning and have it run this script automatically so that by the time I'm at work, the Macs will have updated and rebooted. I'm sure there is a way to do it, but my Google-Fu is failing me.

EDIT/PARTIAL ANSWER Turns out, even if you put TextEdit into Plain Text mode, it's still using 'smart quotes'. Typing out and saving the script through the Terminal and THEN running it worked like a charm.

0942v8653 was kind enough to help me out in chat, and also provided a command to disable this 'feature': defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false

Still looking for a solution to the password issue.

2 Answers 2


The ; and && are interpreted by bash on the local machine. You can see this by running ssh -t env && env: the second time, there will be no SSH_CONNECTION variable.

You can quote the entire command to get it to work properly:

ssh -t $host 'sudo softwareupdate -ia && sudo shutdown -r now'

In my opinion, -t should only allow one, quoted, argument to avoid problems like this…also beware of variable expansion—make sure to put everything in single quotes or escape it with backslashes to keep it from being expanded or interpreted on the local machine.

  • Oddly enough, when I do that I get the error 'sudo: command not found. It baffles me. Although, maybe I should note: I'm running the server.app on Yosemite. I doubt that's the root of the issue, but figured I'd mention it. Commented Oct 23, 2014 at 21:10
  • Are you sure you have that exact line?
    – 0942v8653
    Commented Oct 23, 2014 at 22:06
  • I do indeed. I've eliminated the for loop temporarily just to work out this script, so my current code reads ssh -t [username]@[ClientMacIP] 'sudo softwareupdate -ia && sudo shutdown -r now' Commented Oct 23, 2014 at 22:08
  • Can you try it with a dummy command like echo? This is really weird…
    – 0942v8653
    Commented Oct 23, 2014 at 22:15
  • It's definitely weird. Echo returns the same type of error 'echo command not found. I have to think that it's the way I'm calling the script... I've cd'd into the directory it's in, and am calling it with sudo ./UpdateScript.sh Commented Oct 23, 2014 at 22:18

I am not sure about the reboot issue, but setting up a script to run sudo commands without inputting the password is fairly easy. Take a look at Step 4 of this answer I previously wrote (it was for something else entirely, but the steps to enable running sudo without a password are universal).

Note that actual command to run your script will change to

sudo ./myscript.sh

Step 4: Setting Up sudo to Run Without a Password

Letting the Terminal command sudo run without a password can be very dangerous. That's why the steps above created the shell scripts in their own directory, so what can actually be run is limited.

Enter the following command in Terminal:

sudo pico /etc/sudoers

Then enter your administrator password when prompted.

This may bring you to a (mostly) blank screen, or it may have some text in it. If it's blank - that's fine. You'll just paste the below line at the top. If text already exists, that's also fine; use your down arrow to go right below the lines already in the # User privilege specification section, as seen in the below screenshot.

Here, add the following line:

<yourusername> ALL = NOPASSWD: /Users/<yourusername>/PathToScript/*

In both places where <yourusername> appears, replace it with your Mac username. Press control + x, type y and press return to save and exit.

  • Nice. I'm assuming this is to be done on the server, yes? Not the clients. Commented Oct 23, 2014 at 18:34
  • That is correct.
    – tubedogg
    Commented Oct 23, 2014 at 18:35
  • Hm. It doesn't recognize the command unless I use sudo bash UpdateScript.sh. Moreover, when I do call it like that, it ends up asking me for a password over and over and over until it says "Permission denied (publickey,keyboard-interactive)". Thoughts? Commented Oct 23, 2014 at 18:40
  • When I open the /etc/sudoers file it tells me "This file MUST be edited with the 'visudo' command". You still think pico is safe to use?
    – Fyrefly
    Commented Oct 23, 2014 at 21:11
  • @Fyrefly: Set the VISUAL environment variable to pico.
    – 0942v8653
    Commented Oct 23, 2014 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .