Close program in shell script with pkill - shell

I open a browser in a shell script. After opening the browser, I want to close it in my script with
pkill browser
The problem is commands after opening browser are not executed until I manually close the browser, so I can't close it in the script. Any solutions?

You might also (it does not always work, depending on how many childs the browser forks) record the PID of the started background browser and not use pkill:
browser &
BROWSER_PID=$!
# something
kill "$BROWSER_PID"

How about run the command that invoke the browser in background (&):
open_browser &
# wait .. (ex. sleep 10)
pkill browser

Related

PID for Chrome appwindow is changing

I am launching a new Chrome/Chromium window from the terminal with the --app parameter, trying to get the PID in the process, like so: chrome --app="data:whatever" & PID=$!
This returns a valid PID as long as the browser is not launched. However if the browser already had a window open, I get a message "Opening in existing browser session". There is still a number in the PID, but it has nothing to do with the process/window I just opened. When I run kill -0 $PID, the process is no longer there (but the app window still is of course).
Goal: I just want to know when someone closes the app that was launched form the script, and then execute some cleanup code.

executing a script which runs even if i log off

So, I have a long running script (of order few days) say execute.sh which I am planning to execute on a server on which I have a user account...
Now, I want to execute this script so that it runs forever even if I logoff or disconnect from the server??
How do i do that?
THanks
You have a couple of choices. The most basic would be to use nohup:
nohup ./execute.sh
nohup executes the command as a child process and detaches from terminal and continues running if it receives SIGHUP. This signal means sig hangup and will getting triggered if you close a terminal and a process is still attached to it.
The output of the process will getting redirected to a file, per default nohup.out located in the current directory.
You may also use bash's disown functionality. You can start a script in bash:
./execute.sh
Then press Ctrl+z and then enter:
disown
The process will now run in background, detached from the terminal. If you care about the scripts output you may redirect output to a logfile:
./execute.sh > execute.log 2>&1
Another option would be to install screen on the remote machine, run the command in a screen session and detach from it. You'll find a lot of tutorials about this.
nohup (no hangup) it and run it in the background:
nohup execute.sh &
Output that normally would have gone to the screen (STDOUT) will go to a file called nohup.out.

Opening and closing a process using bash

Using Ubuntu, I would like to create a shell script (bash) for an Ubuntu server, that will open an instance of firefox and then close that specific instance the browser?
To open an instance of firefox, I can write:
firefox www.example.com
I have read that to search for all firefox instances, and to close them manually I can write:
ps aux | grep firefox
pidof firefox
kill #process#
But is there a way for me to search for the specific instance instance of firefox that I opened at the start?
You can use jobs to get the IDs of all running processes started from that shell (e.g. inside your script)
#!/bin/bash
firefox www.example.com &
PID=`jobs -p`
kill $PID
See help jobs for the options. Note that jobs lists all process started from this shell, so if you follow this approach and want to kill multiple processes you might need to do some additional parsing on the output from jobs to find the correct process.
Start the process in the background, and remember its pid.
#!/bin/bash
firefox www.example.com &
declare -i PID=$!
# blah, blah, blah
kill ${PID}
If you're worried about firefox exiting and some other process being assigned ${PID} in the mean time you could change the kill to something like the following to reduce the risk:
ps -p ${PID} | fgrep firefox && kill ${PID}

Multiple process from one bash script [duplicate]

I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
You can just run the script in the background:
$ myscript &
Note that this is different from putting the & inside your script, which probably won't do what you want.
Everyone just forgot disown. So here is a summary:
& puts the job in the background.
Makes it block on attempting to read input, and
Makes the shell not wait for its completion.
disown removes the process from the shell's job control, but it still leaves it connected to the terminal.
One of the results is that the shell won't send it a SIGHUP(If the shell receives a SIGHUP, it also sends a SIGHUP to the process, which normally causes the process to terminate).
And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.
The process won't receive any sent SIGHUP.
Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
Usually used with &(as a background job).
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
You can combine this with backgrounding,
nohup cmd &
and get rid of the output,
nohup cmd > /dev/null 2>&1 &
you can also disown a command. type cmd, Ctrl-Z, bg, disown
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
bg
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)
screen -m -d $command$ starts the command in a detached session. You can use screen -r to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen.

how to send ssh job to background

I logged in to a remote server via ssh and started a php script. Appereantly, it will take 17 hours to complete, is there a way to break the connection but the keep the script executing? I didn't make any output redirection, so I am seeing all the output.
Can you stop the process right now? If so, launch screen, start the process and detach screen using ctrl-a then ctrl-d. Use screen -r to retrieve the session later.
This should be available in most distros, failing that, a package will definitely be available for you.
ctrl + z
will pause it. Than type
bg
to send it to background. Write down the PID of the process for later usage ;)
EDIT: I forgot, you have to execute
disown -$PID
where $PID is the pid of your process
after that, and the process will not be killed after you close the terminal.
you described it's important to protect script continuation. Unfortunately I don't know, you make any interaction with script and script is made by you.
continuation protects 'screen' command. your connection will break, but screen protect pseudo terminal, you can reconnect to this later, see man.
if you don't need operators interaction with script, you simply can put script to background at the start, and log complete output into log file. Simply use command:
nohup /where/is/your.script.php >output.log 2&>1 &
>output.log will redirect output into log file, 2&>1 will append error stream into output, effectively into log file. last & will put command into background. Notice, nohup command will detach process from terminal group.
At now you can safely exit from ssh shell. Because your script is out of terminal group, then it won't be killed. It will be rejoined from your shell process, into system INIT process. It is unix like system behavior. Complete output you can monitor using command
tail -f output.log #allways breakable by ^C, it is only watching
Using this method you do not need use ^Z , bg etc shell tricks for putting command to the background.
Notice, using redirection to nohup command is preferred. Otherwise nohup will auto redirect all outputs for you to nohup.out file in the current directory.
You can use screen.

Resources