Script Works But Need It to Exit Upon Closing Program

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Old 3 Weeks Ago
Script Works But Need It to Exit Upon Closing Program [SOLVED]

Running Xubuntu 16.04 with shell version "GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)," I have a working script that consistently renames a Chrome window:

while sleep 1; do
	xdotool search --name chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id

I can run this script in the background upon boot, but I would prefer to start it upon opening Chrome, then kill it upon closing Chrome.

I've tried a lot of if statements, etc and failed miserably--setting the variables is well above my skills.

May I ask veteran script-ers for assistance in setting up a script that starts the commands listed above when Chrome is opened, then terminates everything when Chrome is closed?

I very much appreciate your patience for my first post, and please let me know if I haven't made things clear enough.

Many thanks.

Edit: listed OS and shell version

Last edited by jakefish; 3 Weeks Ago at 05:12 PM.. Reason: SOLVED
# 2  
Old 3 Weeks Ago
Welcome to the forum.

One option would be to create a wrapper script that, when launched / clicked,

- puts above loop into background, keeping its PID
- runs chrome in foreground until closed
- (softly) kills the PID.
This User Gave Thanks to RudiC For This Post:
jakefish (3 Weeks Ago)
# 3  
Old 3 Weeks Ago
In addition to what my colleague RudiC already said: please always state your OS, its version, your shell, its version and perhaps other relevant properties of your environment depending on your question. Otherwise you will always be asked for that first without getting any (specific) answer and everybody - including you - loses time.

Every UNIX and Linux is - mostly - the same but "mostly" doesn't mean "completely". Since in our work we often deal with the exceptions rather than the defaults we need to know what exactly you are on if we want to give you more then a generalised and generic answer like RudiC did.

I hope this helps.

This User Gave Thanks to bakunin For This Post:
jakefish (3 Weeks Ago)
# 4  
Old 3 Weeks Ago
Thank you for the quick replies and the suggestions for proper posting.

I'm unfamiliar with wrapper scripts, but in theory, is this what I'm looking for:


/home/jake/Scripts_Icons/chrome_retitled &    ##chrome_retitled is the window renaming script (from op)

killall chrome_retitled

This appears to work, in the sense that exiting chrome kills closes chrome completely and terminates chrome_retitled, but I appears to be calling a great many scripts. Since they're simple scripts and all ended upon chrome's closing, is this good enough?

Thanks again for your help.
# 5  
Old 3 Weeks Ago
Not sure the killall will work satisfyingly ... kill $PID will.

Just append the kill command - whichever you use - to the script so it will execute when chrome terminates.
This User Gave Thanks to RudiC For This Post:
jakefish (3 Weeks Ago)
# 6  
Old 3 Weeks Ago
Hey, that did it. Thank you, thank you.

Putting the kill command in the first script alleviates the need for separate kill script. With your help, I'm down to two scripts that successfully close out when Chrome exits.

When I was investigating wrapper scripts, a script-er said bash was needed, but I'm able to run it on the lighter sh .

I went down a rabbit hole with if statements and didn't realize the answer was much simpler.

Appreciate your speedy, helpful replies.
# 7  
Old 3 Weeks Ago
Why two scripts? Unless you have additional things going on that you didn't show above, ALL can be done in one single script.
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Exit script and open program via other user defs Shell Programming and Scripting 0 05-19-2017 08:44 AM
Need to understand how the line in perl program works as shell commend digioleg54 Shell Programming and Scripting 2 05-09-2017 01:18 PM
Could not find the main class: Grasp. Program will exit. cokedude UNIX for Advanced & Expert Users 0 09-09-2013 03:03 PM
Help with Shell Script opening and closing a program plsbbg Shell Programming and Scripting 6 07-29-2013 12:38 AM
C-program works fine interactively, but not on the SGE server Twinklefingers UNIX for Dummies Questions & Answers 1 01-10-2013 11:34 PM
I dont want to exit the program by ctrl + c coolashu Shell Programming and Scripting 2 09-22-2012 06:37 AM
Help with AIX XL C++ complier: app exit before main program jackliang AIX 0 07-18-2011 11:10 PM
Linux:Program exit with displaying a print feature imran721 Shell Programming and Scripting 0 08-24-2010 11:14 AM
Graphical program no longer works after Solaris 10 upgrade Leion Solaris 0 05-25-2010 09:53 AM
Terminal is closing on exit in ksh vij_krr Shell Programming and Scripting 3 02-09-2010 02:46 AM
closing a telnet session on error, in a shell script farahzaiba Shell Programming and Scripting 1 01-23-2009 02:01 PM
closing of ssh section through script GIC1986 Solaris 4 09-24-2008 01:33 PM
running script after closing telnet shinjeck Solaris 2 10-29-2007 07:52 AM
EXIT from a Program not from the function.. ronix007 UNIX for Dummies Questions & Answers 9 09-06-2007 03:12 AM
Line works in solo but not in program? nortypig Shell Programming and Scripting 4 08-21-2006 07:52 AM