How to run raycast script that includes `sudo`

Gwon HongGwon Hong
1 min read

Summary

  1. Write the script that includes sudo “separately”.

  2. Add the /path/to/the/script to some kind of sudo whitelist(sudoer file)

  3. Make a raycast script that includes sudo /path/to/the/script

Example

The script that includes sudo

I wanted to make a shortcut to run the following script (written by me!):

https://gist.github.com/gwonhong/18fd6aab2b3897f269f33586b49c1436#file-homebrew_openjdk_softlinker-sh

but the content isn’t important. Assume that we have an awesome script as follows:

/absolute/path/to/my_sudo_script.sh

#!/bin/bash

sudo rm -rf /

Don’t forget to make the script executable with the following command:

chmod +x /absolute/path/to/my_sudo_script.sh

Add the script to sudoer

First, open the sudoer file with the following command:

sudo visudo # this will open the sudoer file with vim
sudo EDITOR=nano visudo # use this if you prefer nano (like me!)

Then, add the following line at the end of the file:

your_username ALL=(ALL) NOPASSWD: /absolute/path/to/my_sudo_script.sh

To check your username, you can use whoami command.

Write the raycast script that runs the script above

#!/bin/bash

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title type the title you want
# @raycast.mode compact

# Optional parameters:
# @raycast.icon 🍻
# @raycast.author gwonhong
# @raycast.packageName Type The Package Name You Want
# @raycast.description type the description you want

sudo /absolute/path/to/my_sudo_script.sh

Now, you can run the script via raycast!

0
Subscribe to my newsletter

Read articles from Gwon Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gwon Hong
Gwon Hong