Linux how to launch visible terminal with script file automatically at startup - linux

I have seen many ways to launch a script like putting it in profile.D, rc.local, or creating a auto start file but none of those launch the file in a visible window if at all. I need it to be in a visible window in Ubuntu. I need to do this because I am using several emulators to stream to different services, and I don't want to have to start the script on each manually.
I am using visual box for the emulator. The sh file is on a removable drive because it is an external file. I also need it to run as sudo.
Edit: I don't actually need it to run at startup. I just need to have the script run. I can probably just sleep really long for graphic to load.
Edit 2: So I created a service that launched a sh file in /usr/bin/ which was supposed to create a gnome-terminal window that ran my script. It ran, however It didn't create a visible window for some reason. I then tried to specify a display which caused gnome to freak out. Dbus was not launching correctly. another question stated that gnome would not work because of how it was designed and stated to use konsole instead. Konsole also stated that it could not connect to a display, giving a QXcbConnection error. Konsole does not have an option to specify display. I don't know what else to try
Edit 3: So I did the thing in the comment. And the service works. However it only works after I run the file that the service runs in usr/bin manually after every restart. The important parts of the file:
#!/bin/bash
sleep 60
ufw disable
ssh nateguana#$(hostname) -X
xhost +
*launch Gnome**only works after file ran manually*
I have also tried exporting DISPLAY, and changing users with su. I have not tried importing SSHD, as another question said to do, as I think that is only for non local connections. I have also tried every single arrangement of commands possible. Xhost errors stating that it is unable to open display "".

You can use gnome-terminal -e <command> to spawn a new bash terminal which runs the command.
You could use something like
gnome-terminal -e /path/to/bashfile
Bear in mind, this will end the terminal after the bash scipt is done executing.
To avoid this,in a newline add $SHELL to the end of your bash script.
PS: the -e argument is deprecated and might be removed in later versions

Related

Linux xfce desktop launcher: Run a nested command

With xfce4-terminal (on Manjaro): Running chromium "$(xclip -o)" will open the Chromium web browser and visit the content of the clipboard (which is assumed to be a single URL).
I want a desktop launcher to do exactly this. Creating a new launcher (right click on the desktop > create launcher) with the above command in the command field won't work: Chromium is opened, but the address bar says %24%28xclip%20-o%29.
What is happening? The tiny bit of bash knowledge I have tells me that I need to escape some characters -- which ones? I tried different things like chromium \"$(xclip -o)\" and chromium "\$\(xclip -o\)", which produce similar outcomes, but I can't figure it out.
I found this specification of launcher items, but I seemingly fail to understand it well enough to apply it to my problem. Or am I completely on the wrong track?
Another application would be this: xfce4-screenshooter -f -s "$(date +screenshot_%Y-%m-%d_%T.png)" takes a screenshot and names the resulting file with a time stamp. It works being directly run in the terminal, but not when configured as an application shortcut in the keyboard settings. Analogously, the file is named $(date +screenshot_%Y-%d_%m-%T.png).
Try:
bash -c "chromium $(xclip -o)"
in your command field. That worked for me at least. (Also using Manjaro XFCE)

Close google chrome open in app mode via command line

In my python script I am opening chrome in app mode by this command:
google-chrome --app=http://stackoverflow.com
Now I want to be able to close only this running chrome application (I mean if there is another chrome windows with diffrent tabs I don't want to close that, only this that i run). Is this possible and how?
I am using linux.
EDIT:
As far i manage to do something like this:
import subprocess
proc = subprocess.Popen(['google-chrome',
'--user-data-dir=/home/chrome-user', 'http://google.pl'])
# do something
proc.terminate()
And when I call this python script from command line everything is ok. My problem starts when I am running this as a linux service (under /etc/init). I logged and everything is ok except that google chrome seems to not be able to create window or something? I mean there is no error but google chrome window doesn't shows at all.
EDIT 2:
Definitely it is a problem with startup. When i run this on startup:
mate-terminal -e "python3
/path/to/script/script.py"
Everything works fine except that terminal windows is shown. So this somehow solves my problem but if anyone will have got any sugestion what can i do i would highly apreciate it.
as far as I know chrome creates a new process for each tab, killing the process therefore would work. you can kill the process by PID and the PID can be retrieved like this right after starting the process: (in bash) echo $!
Ok so after a long time I come with answer. In linux mint you need to add this to startup programs:
mate-terminal -e "python3 /path/to/script/script.py"
and if you want to run script as root user, this is solution for it (it's very ugly, but works):
mate-terminal -e "bash -c 'echo pass | sudo -S python3 /path/to/script/script.py;$SHELL'"

Linux basics - automating a script execution

When beginning to work, I have to run several commands:
source work/tools
cd work/tool
source tool
setup_tool
Off course, doing this a few times a day is really annonying, so I tried to make a bash script tool where I put these commands and put it in /user/bin to run it with command
tool
However, there is a problem. When i run the script and then try to work by typing some of the tool-based commands, it does not work.
I figured out, that it is fine, since if I make a script and then run it, the script seems to run in the same terminal window, but what it really does is, that it behaves as if it created a "hidden window" for its execution and after termination of the script, the "hidden window" terminates too. So I am asking - is there a way to automatize the source command?
I have tried using xterm -hold -e command, but it runs the programmed script in the new window. Obviously, I don't want that. How can I achieve running it in the current window?
Don't put files like that in /usr/bin. As a general rule you don't want to mess with the distribution owned locations like that. You can use /usr/local/bin if you need a system-wide location or you can create a directory in your home directory to hold things like this that are for your own usage (and add that to the $PATH).
What you've noticed is that when run as a script on its own (tool, /path/to/tool, etc.) that the script runs in its own shell session (nothing to do with terminal windows as-such) and you don't want that (as the changes the script makes don't persist to your current shell session).
What you want to do instead is "source"/run the script in your current session. Which you are already doing with that set of commands you listed (source work/tools is doing exactly that).
So instead of running tool or /path/to/tool instead use source /path/to/tool or . /path/to/tool.
As fedorqui correctly points out you don't even need a script for this anywhere as you can just make a shell function for this instead (in your normal shell startup files .bashrc, etc.) and then just run that function when you need to so that setup.
Be careful to use full paths for things when you do this though since you, presumably, want this to work no matter what directory you happen to be in when you run it.
It doesn't create a new hidden window, nor does it create a terminal. What happens is that if you're running a script, normally it runs on a new shell process. The script you're running is supposed to modify the shell environment, but if you're running the script in a new shell process, that shell process's environment is the one that gets modified, instead of your shell environment.
Scripts that needs to modify the current shell environments usually must be run with the source command. What you need to do is to run the script in the current shell. So you should do source /path/to/tool.
If you want to be able to source the script with just tool, put this in your alias file/shell startup (check your distro doc where the file is, but it's usually either .bash_aliases or .bashrc):
alias tool="source /path/to/tool"

execute a gui application from the command line and send it to the background

Is there a command line utility that I can use for executing X based applications that will detach my applications from the terminal so they aren't closed if the terminal is closed?
I guess such a app could be called something like gnome-run if it existed.
I have tried dtach, but it seems that you have to provide a socket file which is a bit clunky to type. I have also tried nohup, but found that also to be a bit clunky to type by the time std out and err are redirected to /dev/null.
I guess I'm looking for a simple program that will do something similar to this:
#!/bin/bash
nohup $1 > /dev/null 2>&1 &
Yes, there is a way to do it: first you need to run your GUI app and send it to background, then you (probably) want to detach it from Bash task management. For example if I wanted to run gedit this way:
gedit &
disown %1
After that you can close your terminal window and gedit will not be killed. Enjoy!
You already wrote your program, it is called a shell script and you give it the name you like and put it somewhere. Then you either add that directory to your $PATH or in your bashrc you set:
alias gnome-run=<path>/my-awesome-script.sh
Why waste earth's resources on a program?
If you want to run an application (say, gedit) as if it was run from the GUI, use:
xdg-open /usr/share/applications/gedit.desktop
See this answer on superuser.

Screen and Cygwin: no tab completion?

I'm having some problems working with my development environment. Specifically, after I invoke the screen utility from within Cygwin I lose the ability to do tab completion. Before I invoke screen however tab completion works just fine.
I messed around with setting different values for the TERM env variable (VT100, xterm etc) but without success. It must be something trivial but I have no idea anymore. Does StackOverflow have any suggestions for me?
when you issue 'screen' from inside cygwin it might put you in another shell like /bin/sh instead of /bin/bash (and bash is where you're getting the tab completion from).
To fix the problem you could edit your .screenrc file (found in your home directory) and add in this line:
shell bash
Then try running screen again and you should see tab completion work within this new window.
The problem is that bash needs to be run as a login shell in order to have tab completion in the default cygwin setup. If you run bash in a cygwin bash you won’t have tab completion either. To set screen to run bash in login mode, add this line to your ~/.screenrc file:
shell -bash
I had a similar problem with git autocompletion not working when using screen on a linux machine, but wasn't due to a different shell. I fixed it using this question: Git autocomplete in screen on mac os and doing the following:
Get the git autocompletion script
curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -OL
why would you want that hanging around?
mv git-completion.bash .git-completion.bash
add this line to your ./bashrc
source ~/.git-completion.bash
Then in your terminal
source ~/.bashrc
That worked for me.
(I imagine after three years you've probably solved your problem, but I hope this helps someone else)

Resources