You can make a conditional to relaunch the script as root if it's launched as a normal user.
To shutdown the computer:
#!/bin/bash
if [[ $USER == "eka" ]]; then # If the script is ran as "eka" then...
sudo $0 # relaunch it as "root".
exit 0 # Once it finishes, exit gracefully.
elif [[ $USER != "root" ]]; then # If the user is not "eka" nor "root" then...
exit 0 # Once it finishes, exit gracefully.
fi # End if.
shutdown -h +30;
read -p "Press any key to continue... " -n1 -s
Simplified version:
#!/bin/bash
[[ $USER == "eka" ]] && { sudo $0; exit 0; }
[[ $USER != "root" ]] && exit 0
shutdown -h +30;
Very simplified version (not recommended):
#!/bin/bash
sudo $0 # Relaunch script as root (even if it's already running as root)
shutdown -h +30; # Shutdown the computer in 30 seconds.
To suspend the computer:
#!/bin/bash
if [[ $USER == "eka" ]]; then # If the script is ran as "eka":
gnome-screensaver-command -a # Lock computer.
else # Else:
sudo -u eka gnome-screensaver-command -a # Once it finishes, exit gracefully.
fi # End if.
Simplified version:
#!/bin/bash
[[ $USER != "eka" ]] && { sudo -u eka gnome-screensaver-command -a; exit 0; }
Very simplified version:
#!/bin/bash
sudo -u eka gnome-screensaver-command -a
Note: $0
is a variable that holds the full path to the script.
Introduction
The script below puts together several ideas of how gnome-terminal
and bash
shell operate, to create a right-click menuentry for running a selected program in graphical terminal. Of course, this is not double-click via mouse, but still a usable and quick solution. It can be even speedier via right-click and hitting S key to quickly jump into "Scripts" submenu. Additionally, this can work with several selected files and will open terminal window for each one of them.
Demo
Suppose we have a tester script (which merely prints "Hello World" and exits) in some directory opened with Nautilus. Lets assume this script already has executable permissions. Right click on the file , select Scripts -> run_with_terminal.py
Terminal window will appear, and appropriately give the output, and wait till user hits Enter to exit.
Script Source
Also available on GitHub
#!/usr/bin/env python
from os import path
from sys import argv
from subprocess import call
for item in argv[1:]:
full_path = path.abspath('./' + item)
call(['gnome-terminal','-e',
"bash -c '" + full_path + ";read'"])
Privileged version
#!/usr/bin/env python
from os import path
from sys import argv
from subprocess import call
for item in argv[1:]:
full_path = path.abspath('./' + item)
call(['gnome-terminal','-e',
"pkexec bash -c '" + full_path + ";read'"])
Principle of operation
Gnome Terminal ( which is default terminal on Ubuntu ) allows running a command with -e
flag, but problem is that it waits for command to exit. If you have a script or executable that only prints something to the screen and exits immediately, you'll only see terminal window flash and disappear.
Essentially it allows only running one command. On the other hand, if we use bash -c 'command1;command2;command3'
, this will be treated by gnome-terminal as one command, but would allow us in fact to run several as child processes of the bash
shell. This is nothing new and has been shown long before.
Finally, to keep window from immediately exiting, we use read
command that just reads stdin
. Basic use for this is to allow user to close window by hitting Enter key ( sort of adapted trick from using getch()
from C programming in an IDE)
The file manager ( aka Nautilus ) allows use of custom scripts (placed in `~/.local/nautilus/scripts) that operate upon selected file/folder. They're accessible from right clicking on file and selecting appropriate entry in Scripts submenu.
Thus putting all of this knowledge together, we get the working script you see above. Python was merely a language of choice for me, but this same idea could have been implemented in either perl,ruby,shell script, etc.
Best Answer
I tested this and it worked fine. Ensure your script begins with the correct shebang, e.g.
#!/usr/bin/env bash
.Then follow these steps:
dconf-editor
and hit Enter.org ➤ gnome ➤ nautilus ➤ preferences
Click on
executable-text-activation
and from drop down menu select:launch: to launch scripts as programs.
OR
ask: to ask what to do via a dialog.
Close dconf-editor. Thats it!
Alternative way, using terminal: Run:
Note: The file should be executable. You may also need to change permission of file. Right click and set "Allow executing file as program"
Source.