You are not logged in.

#1 2022-01-22 16:54:33

ozynt
Member
Registered: 2021-10-03
Posts: 10

Run a script on specific application startup

Hello I want to run a python script on when I launch chromium and I have not idea how to make this possible. Any ideas or advice?

Offline

#2 2022-01-22 17:25:30

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,999
Website

Re: Run a script on specific application startup

Use a wrapper script at /usr/local/bin/chromium (or ~/bin/chromium for just your user) that runs the Python script then starts chomium.

Something like

#!/bin/sh
python /path/to/script &
chromium "$@"

The ampersand is only needed if the script needs to be backgrounded before chromium starts.

But unfortunately it looks like chromium.desktop calls the full path in the Exec line so you'll also have to copy that to /usr/local/share/applications/ (or ~/.local/share/applications/ for just your user) and change it to

Exec=/usr/local/bin/chromium

Or just

Exec=chromium

Which should also work for menu entries.


Jin, Jîyan, Azadî

Offline

#3 2022-01-22 17:32:49

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: Run a script on specific application startup

You could also create a new desktop file in ~/.local/share/applications/ which executes a shell script that starts Python and Chromium.

That way you can keep both regular Chromium and "Python-enhanced" Chromium around as separate app icons in your launcher.

Offline

#4 2022-01-22 18:09:08

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,464

Re: Run a script on specific application startup

When I saw this, my first thought was also to create a wrapper.  The only other mechanism that came to mind was to create an browser extension that detects startup and runs a script.  Not sure of the details on that, and it would not be able to do something before Chromium starts.

What is it you are trying to accomplish?  Perhaps this is an X-Y problem, and there may be another way to skin this cat.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#5 2022-01-22 19:31:31

ozynt
Member
Registered: 2021-10-03
Posts: 10

Re: Run a script on specific application startup

Okay so Im trying to make a custom discord rpc that starts when I run chromium. So this wwas the best idea I came up with.

Offline

#6 2022-01-23 00:24:14

ozynt
Member
Registered: 2021-10-03
Posts: 10

Re: Run a script on specific application startup

Update: I made a working script but when I close chromium the python script still runs how can I make it auto close when closing chromium?

Offline

#7 2022-01-23 00:44:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,403
Website

Re: Run a script on specific application startup

Assuming you took the above advice, could do the following:

#!/bin/sh

python /path/to/python/script &
pid=$!
chromium "$@"
kill $pid

Of course, a much better approach in my view would be to just make your python script launch chromium, it can then detect when the chromium process exits and the script can finish / exit.

EDIT: sorry for the typo flagged below.  I fixed it above.

Last edited by Trilby (2022-01-23 16:34:30)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2022-01-23 13:56:11

ozynt
Member
Registered: 2021-10-03
Posts: 10

Re: Run a script on specific application startup

Trilby wrote:

Assuming you took the above advice, could do the following:

#!/bin/sh

python /path/to/python/script &
pid=$1
chromium "$@"
kill $pid

Of course, a much better approach in my view would be to just make your pythong script launch chromium, it can then detect when the chromium process exits and the script can finish / exit.

I tried this but the python script is still going after I close chromium.

Offline

#9 2022-01-23 14:01:49

lmn
Member
Registered: 2021-05-09
Posts: 84
Website

Re: Run a script on specific application startup

If I'm not mistaken there is a typo in Trillbys script:
$1 should be $! as it should be the PID of the child and not the scripts first argument.

Edit: added link to documentation

Last edited by lmn (2022-01-23 14:10:04)

Offline

#10 2022-01-23 14:01:53

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,999
Website

Re: Run a script on specific application startup

How about

#!/bin/sh
python /path/to/name_of_script &
chromium "$@"
pkill -x name_of_script

Also:

Trilby wrote:

a much better approach in my view would be to just make your python script launch chromium

+1


Jin, Jîyan, Azadî

Offline

#11 2022-01-23 16:22:38

ozynt
Member
Registered: 2021-10-03
Posts: 10

Re: Run a script on specific application startup

Head_on_a_Stick wrote:

How about

#!/bin/sh
python /path/to/name_of_script &
chromium "$@"
pkill -x name_of_script

Also:

Trilby wrote:

a much better approach in my view would be to just make your python script launch chromium

+1

I gonna do this instead. How do I make my python script a chromium.desktop file then? Or do I just make the execution go for the python script?

Offline

#12 2022-01-23 16:26:41

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 8,999
Website

Re: Run a script on specific application startup

ozynt wrote:

make the execution go for the python script

^ This.


Jin, Jîyan, Azadî

Offline

Board footer

Powered by FluxBB