Close google chrome open in app mode via command line - linux

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'"

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)

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

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

Rescue errors in terminal launched from script

I am using Ubuntu and made a short script on my Desktop. Double clicking it opens a terminal and starts my server.
This is all working fine except there's one problem. If there's an error raised in my server, the launched terminal immediately closes.
I want to keep the terminal open and show the error.
Here's my script:
#!/bin/bash
gnome-terminal -e '/bin/bash -c "cd ~/Desktop/browser_tester/; ./start.sh" '
I'm using the advice from this AskUbuntu answer to construct that command. I'm asking for a way to rescue errors inside the subprocess so that the gnome_terminal displays those errors and doesn't just exit.
You can check the exit status.
./start.sh || read
This will wait for a keypress if the command failed.

Linux (Raspbian) - Running script at login(auto) and keeping window in foreground for interaction

I have tried everything to make this work. login scripts, LXDE-pi autostart, cron task #reboot, init.d, and I cannot get my script running with a terminal window running in the foreground so that I can interact with it. I can get it to run but only in background. Is there any way I can get a script that simply runs: "python /home/pi/myscript.py" at startup and leaves the terminal window open with the script running for my keyboard inputs? I would rather not use the /dev/input/event if at all possible. Thanks
Simply running python /home/pi/myscript.py at startup will run your script without any terminal. So there is no window that can be kept open. The behavior you want can be achieved by starting an terminal application and let it execute your script.
e. g. using xterm:
xterm -e "python /home/pi/myscript.py"
or lxterminal:
lxterminal --command "python /home/pi/myscript.py"
I was missing a simple flag.. what I did was edit ~/.config/lxsession/LXDE-pi/autostart with
#lxterminal -e /home/pi/autoscript.sh
and in that file, I added
cd /home/pi/
python -i 2Trackmain.py
I wasn't using the -i flag, so every time I pressed Enter to move through the interactive py script, it exited the terminal, using -i will keep the window open for your interaction. And I only had to add the change directory part b/c the script called other scripts in the same directory.

Linux KDE Login Script

I've been trying to set up a server that I want to run when I log in (in case the computer crashes or anything like that).
The script works and it runs on log in but it runs in the background or something as when I use ps -A I can see the process running. This is ok but the server is interactive so I want it to start in a terminal window and keep that window open after the user is logged in so that commands can be issued to the server. Is there a way of doing this?
What about starting your script like:
xterm -e YOURSCRIPT
Or if you prefer to use KDE's konsole:
konsole -e YOURSCRIPT
Those open a terminal window to run the specified command after -e.
HTH

Resources