Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Script Works But Need It to Exit Upon Closing Program


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
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  
Replace the script call with the actual while loops?
# 10  
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  
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:
# 12  
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; 03-04-2019 at 02:36 PM..
This User Gave Thanks to jakefish For This Post:
# 13  
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:
# 14  
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:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit script and open program via other user

Hello all.. so i have a problem i need to solve .. #! /bin/bash $SHELL dtterm -title my_prog -e su -user -c 'export DISPLAY=:0.0 ; /path/to/my/prog' & 2> /dev/null $SHELL intr exit This script will work on solaris 10 system in right clikt menu - in a secure system so i need to... (0 Replies)
Discussion started by: defs
0 Replies

2. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Shell Programming and Scripting

Help with Shell Script opening and closing a program

REALLY new to this stuff, sorry. So I want a shell script to open a program, wait 45 minutes, close it, and then do it all again. I want to do this because I am running an iMacros Script for a long period of time and if Firefox is not constantly restarted, memory leaks start to happen. Anyway... (6 Replies)
Discussion started by: plsbbg
6 Replies

4. UNIX for Dummies Questions & Answers

C-program works fine interactively, but not on the SGE server

Greetings, I have a C-program that is made to implement a hidden Markov model on an input file. The program is very memory intensive. I've installed it on my local server where I have an account and it compiles fine. The way they have the server set up is that you can either work... (1 Reply)
Discussion started by: Twinklefingers
1 Replies

5. Shell Programming and Scripting

I dont want to exit the program by ctrl + c

Hey , guys I am new to shell programing ,, so need a help from you guys ... I have to write a shell script that accepts file name or directory name from the user if it is a directory then throw an error if it is a file then give the user two options . 1.overwrite the content 2.append the... (2 Replies)
Discussion started by: coolashu
2 Replies

6. Shell Programming and Scripting

Linux:Program exit with displaying a print feature

hi All I have a scritp running which connects to a local host and then gets a value from a field and then ftp that value to antoher server. It is running fine, and from crontab it gives the output to a file, the problem is sometime it doesnt run but if i check the output file it does not show one... (0 Replies)
Discussion started by: imran721
0 Replies

7. Solaris

Graphical program no longer works after Solaris 10 upgrade

This is a fairly complex issue. I do not have a lot of knowledge on X11. But here are the things. I am running a program called Synergy off a Solaris server. The server sits in a remote network and can be accessed via NAT. Using Putty, I will enable X11 forwarding and launch Synergy via Putty.... (0 Replies)
Discussion started by: Leion
0 Replies

8. Shell Programming and Scripting

Terminal is closing on exit in ksh

hi while executing the following script, my terminal window is getting closed if I enter a invalid option. I want the script should go back the the command prompt. how to do achive it. i execute the script as . ./test #! /usr/bin/ksh Printf " Type of Installer : \n\t\t 1. Whole Build... (3 Replies)
Discussion started by: vij_krr
3 Replies

9. UNIX for Dummies Questions & Answers

EXIT from a Program not from the function..

Hi, I want a command in unix shell script which will exit my whole program, not only from the function it's using. For e.g: $1 == "m_who" && $4 == "Extrnl Vendor" { print "You don have access" exit (0); } If this error is showing on the screen, then nothing should not... (9 Replies)
Discussion started by: ronix007
9 Replies

10. Shell Programming and Scripting

Line works in solo but not in program?

Now I am just getting frustrated and confused... if anyone has some advice on how this anomoly is occurring I would greatly appreciate it. cat helpme.txt | awk 'NR<5{printf("%-20s %-20d %-20d %-20.1f\n","hello",$1,$2,$3)}' | sort -rk4 This line works fine in solo - reads the three fields from... (4 Replies)
Discussion started by: nortypig
4 Replies

Featured Tech Videos