How can I run a set of terminal commands using one click?
Solution 1:
Running it in an apple script is not that bad. you just have to run the entire script as root because sudo requires user interaction. if you do an apple script like this one:
do shell script "cd ~/Desktop/PopcornTV && /path/to/npm install && /path/to/node atv.js" with administrator privileges
replace then/path/to/npm
and /path/to/node
with the output of which npm
and which node
it will open up a dialogue and ask you for your password.
Solution 2:
A solution without using Automator:
1. Gathering some details:
- What is the path to
npm
? Find out usingwhich npm
in Terminal.app - What is the path to
node
? Find out usingwhich node
in Terminal.app - What is your username? Find using
whoami
in Terminal.app
All of this is important, make a note of the outputs.
2. Configuring sudo
Let’s now configure sudo
to not require you to use a password for those two commands.
In Terminal.app, enter the following:
sudo visudo
You will need to enter your password, though you won’t see any typing as you enter it. Press enter when you’ve completed entering your password.
This will open the /etc/sudoers
file for you (probably in vi/vim) press shift+g to go to the bottom of the file, then press o to insert a newline and start inserting text, enter the following:
# Allow me to use npm and node without password
username ALL = NOPASSWD: /path/to/npm /path/to/node
You will need to replace “username”, “/path/to/npm”, and “/path/to/node” with the details you gathered in section 1.
When you have finished entering these details, save and quit by pressing esc then entering :x
then pressing enter.
3. Wrapping it up in a file
Let’s create a file to put those commands in. In Terminal.app enter the following:
echo '#!/bin/bash
cd /Users/username/Desktop/PopcornTV
sudo /path/to/npm install
sudo /path/to/node atv.js' > ~/Desktop/startPopcornTV.command
chmod 740 ~/Desktop/startPopcornTV.command
Again, you’ll need to replace “username”, “/path/to/npm”, and “/path/to/node” with the details you gathered in section 1.
4. Running
On your Desktop you will now have a file called startPopcornTV.command
(depending on your setting it might just be called startPopcornTV
).
All you need to do now is double click that file and it will start PopcornTV for you.
Solution 3:
I think this should work :
echo "#\n cd Desktop \n cd PopcornTV \n sudo npm install \n sudo node atv.js" >> ~/Desktop/startMyServer.command
then make the command file executable
chmod +x ~/Desktop/startMyServer.command
you will then find the file startMyServer.command
on your desktop
Solution 4:
Open up Applescript-Editor
add this:
do shell script "cd ~/Desktop/PopcornTV && /usr/local/bin/npm install && /usr/local/bin/node atv.js" with administrator privileges
Save as an application. Just open it when you want to run.