Open application via command line and kill the terminal

Lobo1980

Member
Joined
Feb 12, 2022
Messages
35
Reaction score
19
Credits
305
Hello.

I want to have a keybinding (shortcut) in the terminal to open thunar as root. In this moment I can do so, but after typing the password the terminal keeps open.

Is there any way to type the password, open the application and kill the terminal automatically?

The command I use is:

Code:
sudo nohup thunar

I have try:

sudo nohup thunar & ; exit;

sudo nohup thunar; exit;

sudo nohup thunar & exit

sudo nohup thunar ; exit

sudo thunar ; exit

Does anybody knows what I'm doing wrong?

Thanks!

OS: Arch updated
WM: Qtile
Terminal: Alacritty / Fish
 


gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
346
Reaction score
410
Credits
3,600
The problem is that, normally, it is the terminal emulator who handles standard input, and the standard and error outputs, and when you kill the terminal, the process is also killed automatically. Roughly speaking, this is because, back in the day, the protocol was designed to hang up when the sender of the standard input, and / or the listener of standard and error outputs, broke the connection. Hence, when you kill the terminal, the protocol hangs up killing the process.

The command nohup stands for no hang up, and by default it only disconnects the standard input by ignoring it. What to do with the standard and error outputs is up to you.

You need to capture the standard output and error streams and send them away from the terminal emulator, either to a file or to /dev/null.

Try this:

Code:
nohup gedit > Downloads/test.out 2>&1 &

If you do this, you'll be able to close the terminal emulator and gedit will survive. What you are doing is:
  1. Telling the system to not hang up the terminal protocol if the terminal that sends the input dies, with nohup
  2. Redirect the standard output to the file Downloads/test.out with > Downloads/test.out
  3. Redirecting error output to standard output with 2>&1. Thanks to the prior, it will end up in the same file.
  4. Launching the whole thing in the background with & --this is just to be tidy, because the process usually survives no matter what but the terminal will ask you for confirmation.
 
Last edited:

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
346
Reaction score
410
Credits
3,600
This is designed to work on headless environments also, when the user logs off (e.g.: when you close your ssh session on a server).

In those environments, launching the command in background is needed as you'll want to log out using the command logout.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,326
Reaction score
1,088
Credits
9,418
It's been a while since I last used thunar, but as far as I remember it has a "run as root" function you can use. You'll have to create it first in "custom actions", though.
Read here https://docs.xfce.org/xfce/thunar/custom-actions#open_thunar_as_root_here
One more thing, the instructions in the link above say to use Command: gksu thunar %f, however, gksu is deprecated, so you should use pkexec instead followed by thunar %f, so it looks pkexec thunar %f.
 

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
346
Reaction score
410
Credits
3,600
I found the last bit for the question using the terminal (probably @Tolkem's solution will do better, but anyways)

You can launch the whole thing with sudo, with the password, detach all the things, and close the terminal automatically, with this:

Code:
echo <your passowrd here without the "<>"> | sudo -S nohup gedit > Downloads/test.out 2>&1 &! ; exit

The only thing is that in the example gedit takes ages (about 15 30 seconds) to show up. I bet this is because by killing the terminal immediately we're messing with the priorities of the command (but that is a bet). Monitoring the whole launch with htop will only show the command stalled at 0.0% CPU and memory for 30 seconds, and then go.
 
Last edited:
OP
Lobo1980

Lobo1980

Member
Joined
Feb 12, 2022
Messages
35
Reaction score
19
Credits
305
It's been a while since I last used thunar, but as far as I remember it has a "run as root" function you can use. You'll have to create it first in "custom actions", though.
Read here https://docs.xfce.org/xfce/thunar/custom-actions#open_thunar_as_root_here
One more thing, the instructions in the link above say to use Command: gksu thunar %f, however, gksu is deprecated, so you should use pkexec instead followed by thunar %f, so it looks pkexec thunar %f.

Thank you very much for your answer. I had already tried that custom action but without much success. The truth is that I am new to Arch and I know that there are many things that I still have to learn. I will enjoy the road.
 
OP
Lobo1980

Lobo1980

Member
Joined
Feb 12, 2022
Messages
35
Reaction score
19
Credits
305
The problem is that, normally, it is the terminal emulator who handles standard input, and the standard and error outputs, and when you kill the terminal, the process is also killed automatically. Roughly speaking, this is because, back in the day, the protocol was designed to hang up when the sender of the standard input, and / or the listener of standard and error outputs, broke the connection. Hence, when you kill the terminal, the protocol hangs up killing the process.

The command nohup stands for no hang up, and by default it only disconnects the standard input by ignoring it. What to do with the standard and error outputs is up to you.

You need to capture the standard output and error streams and send them away from the terminal emulator, either to a file or to /dev/null.

Try this:

Code:
nohup gedit > Downloads/test.out 2>&1 &

If you do this, you'll be able to close the terminal emulator and gedit will survive. What you are doing is:
  1. Telling the system to not hang up the terminal protocol if the terminal that sends the input dies, with nohup
  2. Redirect the standard output to the file Downloads/test.out with > Downloads/test.out
  3. Redirecting error output to standard output with 2>&1. Thanks to the prior, it will end up in the same file.
  4. Launching the whole thing in the background with & --this is just to be tidy, because the process usually survives no matter what but the terminal will ask you for confirmation.

Thank you very much for the command and especially for the explanation of what it does. Right now it didn't work for me because the terminal stays open, but I liked reading it because I can use that same application in the future.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,326
Reaction score
1,088
Credits
9,418
Thank you very much for your answer. I had already tried that custom action but without much success. The truth is that I am new to Arch and I know that there are many things that I still have to learn. I will enjoy the road.
You're welcome. Well, it shouldn't really matter which distro XFCE is installed on, I think. How do you mean "without much success"? I started EndeavourOS live session in QEMU, launched thunar, created the custom action, and it worked for me. Check it out:
1649908344091.jpeg


1649908483334.png


1649908373917.png


1649908415471.png


1649908589410.png


Two things:
1. While creating the custom action(pic1)I forgot the %f, it worked nonetheless, as you can see in the last pic.
2. Make sure to go to Appearance Conditions tab and check Directories(pic 2)

Hope this helps! :)
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!


Top