1

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.

bercik
  • 816
  • 1
  • 9
  • 18

2 Answers2

0

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 $!

Steven Stip
  • 387
  • 3
  • 11
  • Unfortunatelly when type echo $! in console there is no output. – bercik Jan 12 '16 at 12:43
  • when I run it after starting top I see: 25888 [1]+ Stopped top How are you starting chrome in python? It might already return the pid or is even started as a subprocess – Steven Stip Jan 12 '16 at 13:47
  • 1
    Ok i manage to call it as subprocess of another user, than i can call subprocess.terminate(). But thanks for your reply. – bercik Jan 12 '16 at 13:59
  • But unluckilly now i have got another problem. – bercik Jan 12 '16 at 14:20
0

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

bercik
  • 816
  • 1
  • 9
  • 18