Score:1

How to execute my code using terminal without terminal being occupied

sh flag

Guys you might think this is a coding issue but there is a strong possibility that I wouldn't need to entirely modify my code to solve this problem.

So I've written a headless(without GUI) music player using python and I am currently executing it like this: python3 main.py I can pause/unpause the running track from inside the program. but the terminal is occupied , I have to let this extra terminal remain open while I am doing whatever I'm doing and It's kind of bugging me.

How I interact my program now: the terminal is occupied so I should terminate my program to free it

:~$python3 main.py
playing track0.mp3: pause 
paused track0.mp3: quit
:~$ls #I terminated the program so I could do this in currently running terminal

How I want to interact with my program:the terminal is NOT occupied so I can run other stuff in it.

:~$python3 main.py -init #It should not open the program in terminal
:~$ls #so I can do this while the music is playing
:~$python3 main.py pause #and like this I pause the program
:~$nano ~/.bashrc #and again I can use my terminal 
:~$python3 main.py quit #and like this I terminate my program

I know it's possible I just don't know how.

Edit: A person said you should publish your code so we can help you so : https://github.com/yolowex/odd-musicplayer

YoloWex avatar
sh flag
You are right . I am adding the source code to my git.
raj avatar
cn flag
raj
You can try running your program inside `screen`, then detach the `screen` session (Ctrl-A then D). Whenever you want to return to your program, you can re-attach the `screen` session (`screen -r`). Or simply open two `screen` windows and switch between them. Check the `screen` manual: https://linux.die.net/man/1/screen
Score:3
vn flag

You should realize that the Linux terminal (bash) is capable of multitasking, so the terminal is never truly "occupied". By default you can suspend a running program with Ctrl + Z (sending stty susp).

But it can be even simpler to run the script in the background.

python3 main.py &

You can then later give it foreground focus with the fg command. This is basic job control.

If you additionally want to run the script so it isn't attached to the current terminal (and doesn't exit when you close it), use the nohup command:

nohup python3 main.py &
YoloWex avatar
sh flag
I tried & / disown and nohup but none of them seem to work. exactly nothing happens. I think this is because my code includes input commands and a thread so it is a bit trickey. to exit the program properly I even needed to use thread.daemon so the thread is killed after terminating the program. I also tried ```Ctrl```+```Z```
Artur Meinild avatar
vn flag
Ok, in that case [so] is the right place to ask.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.