IntelliJ Idea - Run sudo bash script as external tool

How to run a bash script that requires elevated permissions as external tool in Jetbrains IntelliJ Idea.

Running a bash script that requires root privileges as an external tool in IntelliJ Idea could be tricky. The execution might run into errors or displays a password prompt without hiding the entered characters. That's not nice.

The following test script shows how one might write a bash script that requires root privileges. What it does first is printing the username on the console using whoami. Running whoami will display the username of the user logged in for the current session.

It then tests whether the user is already root or not. If not, then the absolute path of the script is determined and the script is called again using sudo "$script_path" "$@" with the arguments previously provided.

my_sudo_bash_script.sh

#!/bin/bash
# Print who you are
whoami
# Check if user is root. 
# If not: rerun script @ script_path with args ($@) as root
if [ $(id -u) != "0" ]; then
    script_path=$(readlink -e -- "$0")
    echo "This script requires root permissions"
    sudo "$script_path" "$@"
    exit $?
fi

After making the script executable with chmod, we're able to run it:


cd ~/testdir
chmod +x my_sudo_bash_script.sh
./my_sudo_bash_script.sh

First, whoami writes the current username to the console. Then you will be asked for the root password.


joerg
This script requires root permissions
[sudo] Passwort für joerg: 

After entering the password, the script is called again. whoami will now write root to the console.


joerg
This script requires root permissions
[sudo] Passwort für joerg: ********
root

Problem

IntelliJ Idea allows users to Configure third-party command-line applications as external tools to run them from IntelliJ IDEA [1]. To create a new external tool follow the instructions described on Jetbrains "External tool settings" page [2].

For this demonstration the settings are as follows:

IntelliJ Idea: External tool settings [2]

To run the new tool, right click on the editor area and choose Bash scripts | Testscript. This leads to some ugly error messages:


joerg
This script requires root permissions
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required

Another solution that uses a wrapper bash script to run sudo first results in an unsatisfactory password prompt that doesn't hide the password:

Using a wrapper script to run sudo

Solution

The solution for me was to write a python wrapper script (my_sudo_bash_script_wrapper.py) and use it to execute the bash script. In the example given below, the bash script name is hardwired. This can of course be changed so that the wrapper is able to run arbitrary bash scripts whose names/locations are then passed to the wrapper as arguments.

The gnome-terminal is a terminal emulator used by default on GNOME-based Linux desktop environments such as Ubuntu, Fedora, and Debian. This means that the solution may not work for you if the above requirements are not met.

Python

import os, 
import subprocess
import sys

def start_bash_script_in_terminal(script_name, script_args):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    script_path = os.path.join(current_dir, script_name)
    arg_str = ' '.join(script_args)
    command = f'bash {script_path} {arg_str}'
    subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', command])

if __name__ == "__main__":
    bash_script_name = "my_sudo_bash_script.sh"
    script_args = sys.argv[1:]  # Python arguments passed to the script
    start_bash_script_in_terminal(bash_script_name, script_args)

The settings in "External tool settings" must now be changed as follows:

IntelliJ Idea: External tool executes the python wrapper script

If the external tool is now executed, a terminal window opens and the bash script is executed there.

References

  1. Jetbrains, External tools, Accessed on Sep 01, 2023. [Online]. Available: https://www.jetbrains.com/help/idea/2023.2/settings-tools-external-tools.html
  2. Jetbrains, External tools settings, Accessed on Sep 01, 2023. [Online]. Available: https://www.jetbrains.com/help/idea/2023.2/settings-tools-external-tools.html

Did you like the content? If you want to do something good for me, you can pour a little coffee into my empty pot.
Just click the PayPal.Me-Button* below. Thank you very much!

*For more information on our PayPal.Me link, see Impressum & Datenschutz