Do GUI based application execute shell commands in the background?
Do these GUI-based applications also execute the same command in the background?
Yes and no. They write to the dconf
database of settings, but they could be using different ways to do so. Programs written in Python will likely use the gi.repository.Gio
module (I know because I use it a lot) or they can instead use gsettings
as an external command by calling subprocess.Popen(['gsettings','org.some.schema','some-key','value'])
, and it will basically run as a shell command. A C program will use something similar, likely a gio.h
library, or it could even use the exec()
family of functions to do the same as Popen
does in python. So to answer your title question: "Do GUI based application execute shell commands in the background?" They can, but likely it's not necessary because there's a library for whatever language that app is written in, and likely it's gonna be a bit faster to use a library function, than spawning a new process.
To give you a sample of how it's done with libraries/modules, feel free to take a look at the source code of my launcher list indicator. There I have written a function to create an instance of the Gio.Settings
class and then use it to modify the Unity launcher depending on the type of list you want to have there.
How to have a peep at the internal working of these applications? I mean, is there any way of actually looking at the commands that are being executed at every click of button?
No. If you want to see which command is issued in that app's programming language as you press a button or click on window elements, then it's not possible. Read the source code of application, if it is possible to obtain it. You can use dconf watch /
to see what settings are being changed, but not how it's done.
Technically, if you know how to operate a debugger, read memory addresses, and know some assembly language, then you can know what an app does on the CPU and memory level. This is known as software reverse engineering and is frequently used by security professionals to analyze malicious software and discover vulnerabilities in legitimate software.
Do these applications open up a terminal in the background and execute these commands?
No, there's no terminal attached. Many programs know where the dconf
database for the user is located and write there. There's also an inter-process communication bus known as dbus
, where programs can send signals, and a program will be like "Hey, that's a message for me!"
Addendum
-
Can applications run other applications ? Yes, that's done via standard
fork()
andexecve()
system calls. The essence of creating processes on Linux and other *nix systems is largely based on these two. Shell mechanism for running non-builtin commands uses that a lot in particular. When you run interactively$ ls
the shell will create a new process via
fork()
, that process will runexecve()
which will startls
. Because of howexecve()
that new forked process will bels
. Thepipe()
system call is what will help read back the output ofls
. I strongly suggest reading my answer for What is the difference between pipe and redirection to understand how pipe mechanism works - it's not just|
operator, but actually a syscall. Can applications run shell commands ? No. Shell syntax is only understood by shell itself. What you can do, however, is start a shell with a command-line
-c
switch and provide appropriate commands. This is often used for custom shortcuts set in GNOME or other desktop environments, since custom shortcuts operate on executables and there is no shell to understand the syntax. Thus as an example, you'd dobash -c 'xdotool key Ctrl+Alt+T'
to indirectly runxdotool
command orbash -c 'cd $HOME/Desktop; touch New_File'
to create new file on desktop via shortcut. This is particularly interesting example as you can use a shell variable, since you are using a shell explicitly.
Spying on what happens
Most of what these settings editors do can be watched by running
dconf watch /
in a terminal.
gsettings
Also most of the time, to achieve what you see happening with the command above, these applications will need to edit the dconf
database (further below). This can be done either directly, by using the cli options of dconf (which is not preferred), or by running the corresponding gsettings
commands, like the one you mention.
To run these commands, no terminal window is needed, as you can see in the examples.
About, gsettings, dconf and the dconf database
gsettings
is the cli frontend to dconf
, which in its turn edits the dconf
database, where most of the settings are stored, in binary format. See also this nice answer.
The dconf
database, by the way, can also be edited from the GUI by dconf
editor, which is in the repositories:
Working samples
a. In python
To show you what happens under the hood, below a working sample to toggle your launcher position from GUI in a single (toggle) button:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import subprocess
key = ["com.canonical.Unity.Launcher", "launcher-position"]
class ToggleWin(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Toggle")
button = Gtk.Button("Toggle launcherposition")
button.connect("clicked", self.toggle)
self.add(button)
def toggle(self, *args):
# read the current setting on launcher position
current = subprocess.check_output([
"gsettings", "get", key[0], key[1]
]).decode("utf-8").strip()
# toggle to the other option
new = "'Left'" if current == "'Bottom'" else "'Bottom'"
subprocess.Popen([
"gsettings", "set", key[0], key[1], new
])
def delete_actions(*args):
Gtk.main_quit()
def miniwindow():
window = ToggleWin()
window.connect("destroy", delete_actions)
window.show_all()
Gtk.main()
miniwindow()
-
Paste the code into an empty
file.py
-
run it by the command:
python3 /path/to/file.py
...and have fun.
b. Launcher icon
Even a simple launcher can do the job from the GUI:
[Desktop Entry]
Name=Set launcherposition
Exec=zenity --info --text="Right- click to set launcher position"
Type=Application
StartupNotify=False
Icon=preferences-system
Actions=Launcher to bottom;Launcher on the left;
[Desktop Action Launcher to bottom]
Name=Launcher to bottom
# right click option to set launcher to bottom
Exec=gsettings set com.canonical.Unity.Launcher launcher-position Bottom
[Desktop Action Launcher on the left]
Name=Launcher on the left
# right click option to set launcher to left
Exec=gsettings set com.canonical.Unity.Launcher launcher-position Left
- Paste the code into an empty file, save it as
setlauncher.desktop
- Drag it on to the launcher and right-click
For permanent use, store it in ~/.local/share/applications
(for local use) or ~/usr/share/applications
for all users.