Script Works But Need It to Exit Upon Closing Program


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 2 Weeks Ago
Hmm, then I'm still a little lost.

First script:

Code:
#!/bin/sh
while sleep 1; do
	xdotool search --name Chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id
	done
done

Second command:

Code:
google-chrome-stable

Third command:

Code:
killall chrome_retitled

Right now, I'm running:

Code:
#!/bin/sh
/home/jake/Scripts_Icons/chrome_retitled ##chrome_retitled = first script## &
google-chrome-stable
killall chrome_retitled

Could you show me how to put all three things into a single script?

Thanks once more.
# 9  
Old 2 Weeks Ago
Replace the script call with the actual while loops?
# 10  
Old 2 Weeks Ago
I posted here, thinking that would probably be the way. But my scripting skills are not up to inventing such variables.

But I'm relieved that there isn't an obvious way to simply string those three things together--I spent much time moving them around, but I always ended up having to call the one while script, running that in the background (as you suggested), then executing the two commands, one after the other, in a separate script.
# 11  
Old 2 Weeks Ago
What RudiC is trying to show you is the following: you have one script:

Quote:
Originally Posted by jakefish
Code:
#!/bin/sh
while sleep 1; do
	xdotool search --name Chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id
	done
done

And in another script you call it and put it in the background:

Quote:
Originally Posted by jakefish
Code:
#!/bin/sh
/home/jake/Scripts_Icons/chrome_retitled ##chrome_retitled = first script## &
google-chrome-stable
killall chrome_retitled

In fact you need to take out your comments or at least put the "&" before the comment signs, otherwise it is just part of the comment, but that only as an aside. So, in fact, you have:

Code:
#!/bin/sh
/home/jake/Scripts_Icons/chrome_retitled  &
google-chrome-stable
killall chrome_retitled

The line i marked bold is where you call the first script, right?

Now, a script is basically a list of commands, bput together into a file. You can do that inside a shell too, using what is called "process substitution". You write ( .... ) and what is inside the braces is considered one command to the outside. You can redirect its input or output and everything else you do with a separate script - and that includes putting it in the background:

Code:
#!/bin/sh
( while sleep 1; do
	xdotool search --name Chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id
	done
done) &
google-chrome-stable
kill -15 %1

Notice that "%" is using the job control of the shell: if you have background jobs they are assigned consecutive small numbers which you can list with the jobs command. You can address the processes using these small numbers instead of their PIDs.

Furthermore notice that there is a difference between "process substitution" (( ... )) and "command substitution" ($( ... )) as they are easily confused. They (can) serve similar purposes but are different things.

You should NOT use killall anyway, because this sends signal 9 to the process(es). You should always try signal 15 first and only use signal 9 if that doesn't work.

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
jakefish (2 Weeks Ago) RavinderSingh13 (2 Weeks Ago)
# 12  
Old 2 Weeks Ago
Code:
#!/bin/sh
( while sleep 1; do
	xdotool search --name Chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id
	done
done) &
google-chrome-stable
kill -15 %1

Your instructions are extraordinarily clear and helpful. First off, you combined things into a single script that almost terminates itself appropriately, which is close to what I was hoping for. Secondly, your explanations for why/how parentheses are used make sense and I see how I can apply the concept to other scripts I'm working on.

While I understand perfectly why users might choose
Code:
kill -15

rather than the more aggressive
Code:
killall

it turns out that
Code:
kill -15 %1

doesn't work in the script. This script, called "chrome," appears to terminate itself successfully:

Code:
#!/bin/sh
( while sleep 1; do
	xdotool search --name Chrome 2>/dev/null | while read id; do 
		xdotool set_window --name "Chrome" $id
	done
done) &
google-chrome-stable
killall chrome

Because of your clear explanation, I was able go into xubuntu's task manager and see that the script called "chrome" generates itself several times during actual browsing. Before exiting the chrome browser, you can watch "chrome" kill itself until there're only two "chrome"'s running, but it never gets down to one "chrome."

Is that why
Code:
killall

is necessary here? Because there are multiple "chrome"'s? I hope I'm right, because that would mean you've taught me something that I used to troubleshoot on my own.

Granted, the pedagogy here is small, but folks like me have to start somewhere.

Last edited by jakefish; 2 Weeks Ago at 01:36 PM..
This User Gave Thanks to jakefish For This Post:
vgersh99 (2 Weeks Ago)
# 13  
Old 2 Weeks Ago
Quote:
Originally Posted by jakefish
Is that why
Code:
killall

is necessary here? Because there are multiple "chrome"'s?
Yes, exactly. The command kill sends a signal to a (exactly one) process, identified by the given process ID. Issue a

Code:
kill -l

to see a list of signals your system is able to send. killall, on the other hand, works on groups of processes: it will send the same signal (if i remember correctly signal 9) to all processes identified by a common command. You can try the difference with a little experiment: Issue the command

Code:
sleep 1000 &

three times. In the ps -er | grep sleep output you will see three times the same process "name" (in fact the command, as processes have no names, just IDs) but with three different PIDs, like this:

Code:
$ ps -fe | grep [s]leep
bakunin      5897  5893  0 21:12 pts/0    00:00:00 sleep 1000
bakunin      5898  5893  0 21:12 pts/0    00:00:00 sleep 1000
bakunin      5899  5893  0 21:12 pts/0    00:00:00 sleep 1000

You can terminate each one of them by kill -15 <PID>, but killall sleep will first search for all processes with a command "sleep", find these three and then send one after the other a signal.

Web browsers traditionally wreak havoc in the process model for reasons i have not fully figured out. I.e. it is not possible to start a Firefox (or Chrome, for that matter) two times, like any other program. And each window (i hate tabs, so my browser instances are all windows) is not a separate process but - honestly, i have given up on trying to understand. I all programs would behave that way i'd already asked for a permanent place in the asylum. Why a process started once has two processes instead of one i don't understand. If you are interested - and i hope you are, because this is what makes you learn more and more - i suggest you investigate and tell us if you find out. I don't have this xdotool you are using.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
jakefish (2 Weeks Ago)
# 14  
Old 2 Weeks Ago
I guess that all the chromes you see "several times during actual browsing" are child processes created / spawned by the parent chrome. When killing that, all child processes will disappear as well.
Why use kill at all? Check the existence of the parent process in the while loop (pgrep available?), and quit it when that process exits. (Untested!) example:


Code:
( sleep 1
  while pgrep google-chrome-stable
    do  xdotool search --name Chrome 2>/dev/null | 
        while read id
           do xdotool set_window --name "Chrome" $id
           done
        sleep 1
    done
 ) &
google-chrome-stable

Adapt the first sleep's interval to allow for the chrome process creation.
This User Gave Thanks to RudiC For This Post:
jakefish (2 Weeks Ago)
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