LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 07-02-2021, 12:21 PM   #1
Snugbug
Member
 
Registered: May 2020
Posts: 97

Rep: Reputation: Disabled
Kubuntu 21.04 - How to close terminal automatically after bash is done?


If I have a bash file like this

Code:
#!/bin/bash
some commands...
more commands...
exit
the exit command ...doesn't work, so the terminal doesn't close.

exit 0
exit 1
exit N
&& exit , after the last command, none of these are working.

The strange thing is, all distros I know so far do support the exit command. Why not Kubuntu?

How can I make the terminal close automatically(!) after the bash is done?

EDIT:
Never mind!
I've got it!
The required command is:
Code:
kill -9 $PPID

Last edited by Snugbug; 07-06-2021 at 01:47 PM.
 
Old 07-03-2021, 02:36 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Snugbug View Post
If I have a bash file like this

Code:
#!/bin/bash
some commands...
more commands...
exit
the exit command ...doesn't work, so the terminal doesn't close.

exit 0
exit 1
exit N
&& exit , after the last command, none of these are working.

The strange thing is, all distros I know so far do support the exit command. Why not Kubuntu?

How can I make the terminal close automatically(!) after the bash is done?

EDIT:
Never mind!
I've got it!
The required command is:
Code:
kill -9 $PPID
Sorry, but no.
That's not how you exit a script.
kill -9 should be used only as a very last resort and probably never inside a script.

When a script is called in a terminal window, and the script exits, the terminal should(tm) close.
If it doesn't it might be something in the emulator software's settings preventing that.

If indeed your script does not exit, then we need to look at the script.
 
Old 07-06-2021, 11:03 AM   #3
Snugbug
Member
 
Registered: May 2020
Posts: 97

Original Poster
Rep: Reputation: Disabled
I'm talking about some youtube-dl scripts, where for example, I tried to toggle two target folders, previously selected in youtube-dl.conf, located in ~/.config .

I created two files called youtube-dl.conf and youtube-dl2.conf .
The 1st one looks like this:
Code:
# --output "~/Videos/youtube-dl/%(title)s.%(ext)s"
--output "/media/<username>/BACKUP/yt-dl-Zielort/%(title)s.%(ext)s"
And the 2nd one like this:
Code:
--output "~/Videos/youtube-dl/%(title)s.%(ext)s"
# --output "/media/<username>/BACKUP/yt-dl-target/%(title)s.%(ext)s"
BACKUP is the root folder of a 2nd HDD (an internal one, not USB!).

I toogle the to required paths by renaming the two files with the bubble sort method.

Code:
#!/bin/bash
echo
echo
echo "Toogle target folder in youtube-dl.conf"
echo "***************************************"
echo
echo "Target folder in 'youtube-dl.conf' is currently :"
echo
sleep 1
cd /home/<username>/.config
echo "$(grep -v '#' ./youtube-dl.conf)"
sleep 2
clear
echo
echo
echo "   Target folder in 'youtube-dl.conf'"
echo
echo "         ...will be swapped !"
echo
echo "         ...Please wait..."
sleep 1
mv ./youtube-dl.conf ./youtube-dl3.conf
# sleep 1
mv ./youtube-dl2.conf ./youtube-dl.conf
# sleep 1
mv ./youtube-dl3.conf ./youtube-dl2.conf
# sleep 1
clear
echo
echo
echo "   Target folder in 'youtube-dl.conf'"
echo
echo "         ...has been swapped !"
sleep 1
clear
echo
echo
echo "           It is now :"
sleep 1
echo
echo "$(grep -v '#' ./youtube-dl.conf)"
echo
sleep 2
echo
kill -9 $PPID
As I said before, none of the exit commands I found was working. The kill command was in fact the last chance for me to get the terminal closed.

But it is not only related to my youtube-dl scripts, after none of the bashes the terminal is closed when they are done.

Last edited by Snugbug; 07-07-2021 at 11:23 AM.
 
Old 07-07-2021, 02:18 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Snugbug View Post
As I said before, none of the exit commands I found was working. The kill command was in fact the last chance for me to get the terminal closed.

But it is not only related to my youtube-dl scripts, after none of the bashes the terminal is closed when they are done.
As I said before:
Quote:
Originally Posted by ondoho View Post
When a script is called in a terminal window, and the script exits, the terminal should(tm) close.
If it doesn't it might be something in the emulator software's settings preventing that.
The 'kill -9 $PPID' in your script is a horrible kludge. Your real problem is elsewhere, and I suspect your terminal is somehow set to stay open after the calling script is done.

Try installing a different terminal emulator and see if the problem persists when you use it to run your scripts.
 
Old 07-07-2021, 07:42 AM   #5
Snugbug
Member
 
Registered: May 2020
Posts: 97

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
As I said before:


The 'kill -9 $PPID' in your script is a horrible kludge.
I absolutely agree, but I didn't find any other solution so far.


Quote:
Originally Posted by ondoho View Post
Your real problem is elsewhere, and I suspect your terminal is somehow set to stay open after the calling script is done.

Try installing a different terminal emulator and see if the problem persists when you use it to run your scripts.
Anything you can recomend there, by any chance?

I didn't do anything with the ordinary Kubuntu terminal, but to add a bit transparency and to add neofetch, but neofetch should be done way before the bash even begins, shouldn't it?
 
Old 07-07-2021, 10:05 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
what is much better: use
Code:
source <your script>
and the exit in your script will [most probably] close your terminal. If not you need to explain your situation better.
also you may try:
Code:
exec bash <your script>
by the way: you can safely use
Code:
grep -v '#' ./youtube-dl.conf
# instead of
echo "$(grep -v '#' ./youtube-dl.conf)"
 
Old 07-07-2021, 11:24 AM   #7
Snugbug
Member
 
Registered: May 2020
Posts: 97

Original Poster
Rep: Reputation: Disabled
The biggest problem is that I'm no programmer. That's why I google for pieces of code and put them together until I get something that's somehow doing what I want.
 
Old 07-08-2021, 08:46 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by pan64 View Post
and the exit in your script will [most probably] close your terminal. If not you need to explain your situation better.
That's exactly what's happening, and OP already specified it happens with all scripts.

My suspicion: konsole (?) is somehow set up to stay open after execution, similar to xterm's '-hold' option.
But I do not use konsole so cannot advise more than to look through its preferences.
Apparently that's too complicated for OP because they're "not a programmer" - well, neither am I.

OP; one more idea:
How exactly are you launching all these scripts? Not from a command line I presume?
 
Old 07-08-2021, 11:02 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,606

Rep: Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549Reputation: 2549
Quote:
Originally Posted by ondoho View Post
How exactly are you launching all these scripts? Not from a command line I presume?
They answered that in the other thread...

(Snugbug: next time subjects collide, answer relevant questions in the correct thread, and post a link in the other thread.)

Quote:
Originally Posted by Snugbug View Post
Usually, when I double click a bash for example in Mint, I'm asked if I want to execute this bash or to show its content. Then I can choose between "Run in terminal", "Show", "Cancel" and "Run". This is almost the same with all the distros I know so far.

But in Kubuntu, I have to start a bash with a right click and select "Run in terminal". Then, the terminal doesn't close automatically in the end, anymore.
Otherwise, I mean by double clicking, I get only the content of the bash, being shown in Kate.
Anyway, the issue is not that exit/Bash doesn't work, it is (as suspected) the way Konsole is being called.

On my Debian+KDE, Dolphin doesn't have a "Run in terminal" option but it does have a "Run in Konsole", under "Actions". (I've no idea if Kubuntu is different or if Snugbug typed the wrong thing.)

It is provided by "/usr/share/kservices5/ServiceMenus/konsolerun.desktop", which starts:
Code:
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/x-executable;
Actions=runInKonsole;
X-KDE-AuthorizeAction=shell_access

[Desktop Action runInKonsole]
TryExec=konsole
Exec=konsole --hold -e %f
Icon=utilities-terminal

...
After editing that file to remove the "--hold" from the Exec line, running scripts from the GUI now allows Konsole to close afterwards.

If Kubuntu does things differently, there may be an alternative .desktop file responsible, but it shouldn't be difficult to locate.

 
Old 07-08-2021, 04:16 PM   #10
lovemeslk
Member
 
Registered: Feb 2020
Location: Rantoul IL
Distribution: Slackware
Posts: 350

Rep: Reputation: 72
if bla bla bla
else bla bla
fi
exit

exit doesn't work.
Then file a bug report with your OS.
https://help.ubuntu.com/community/ReportingBugs

Last edited by lovemeslk; 07-08-2021 at 04:19 PM.
 
Old 07-09-2021, 05:47 AM   #11
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
I have Kubuntu 20.04 which provides Dolphin 19.12.3.

Settings > Configure Dolphin > Confirmations > When opening an executable file shows three options:

Always ask
Open in application
Run script

I have "Run script" set. (You can, as a cautionary measure, go with "Always ask" and then choose "Execute".)

Here's a test script, touch.sh, which I made executable. Its contents:
Code:
#!/bin/bash

touch $(date "+%Y%m%d%H%M%S").txt
When I'm in Dolphin and in the folder containing touch.sh, if I double-click (or single click depending on how you have your system set) touch.sh, the script runs and a time-stamped file is created in the folder. Konsole is not involved. No terminal opens or needs to be closed.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Spectre and Meltdown: Explanation, Info, What's being done, What can be done Zyblin Linux - Security 7 02-17-2018 09:48 PM
close terminal mean close background process? Niceman2005 Linux - General 7 05-27-2010 02:58 PM
How to launch an app in terminal, & keep it running after I close terminal? kornerr Linux - General 7 06-24-2006 05:54 PM
suse 10.0 => processes "sending kill signal": "done, done, missing"...!? ungua Linux - Newbie 2 01-18-2006 10:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 11:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration