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.
Related
From within a Java application on macOS, I use Runtime.getRuntime().exec("open -Wn filename") to launch a file with its Default application, lets call it the Viewing application (for example AdobeReader for pdf). That works fine.
My issue arises, when I want to close the viewing application (for example AdobeReader).
The problem is, that the open command itself is launched as a child process of the java application, but the open command launches the viewing application not as a child process, but as child of launchd(1). As a result, when I destroy the process from the Java application, only the open process is killed, but not the viewing application.
So far I could not manage to get a PID of the viewing application process to be able to kill it. With a ps I can only find it, when I have the application name, but that is exactly what I do not have, since I want to let the os decide about the viewing application.
Does anybody have an idea how I could
get the pid of the application that is launched from the open command, without knowing the applications Name or UTI(remember, open is not the parent process of the viewing application)?
or
make the launched application a child of the open process, so I can kill it by killing the open process?
or
any other possible solution?
Your ideas are very much appreciated.
I found a solution by getting the pid from the lsof command, since I know the filename:
lsof -t filename
Having the pid, I can kill the process, means the Viewing Application:
kill $(lsof -t filename)
The full solution looks like this:
String killCommand = "kill $(lsof -t " + filename+ ")";
ProcessBuilder builder = new ProcessBuilder("bash", "-c", killCommand);
builder.start();
Not very pretty, but it does the job.
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
TL;DR How can I get a bg (background) job into fg (foreground) after I close the terminal window?
FULL I'm using terminal to start Spotify and put it into background with CTRL+Z and bg. Everything works as expected within this window: fg puts the process again in forground and jobs shows me Spotify as a running process. The problem occurs if I close the terminal window.
After I close the terminal window and open a new terminal window, the jobs command can't find Spotify running in the background. Therefore I can't access the process for stopping or closing anymore.
What is the advantage of this behaviour CTRL+Z, fg, bg & jobs ? And how can I get back my process?
You can search your old process via ps -A. Or ps -A | grep <application name>.
If you found it, then use reptyr <pid> to get the application on your new console.
But if you closed the old terminal, all the terminal applications will probably be closed also, so it's too late.
Use screen, before you start your app. This will help you to reconnect to your session from another terminal.
The job numbers only refer to background processes running under your current shell. The shell couldn't know the job numbers associated with the old shell.
This hopefully should be an easy question to answer. I am attempting to have mumble-ruby run automatically I have everything up and running except after running this simple script it runs but ends. In short:
Running this from terminal I get "Press enter to terminate script" and it works.
Running this via a cronjob runs the script but ends it and runs cli.disconnect (I assume).
I want the below script to run automatically via a cronjob at a specified time and not end until the server shuts down.
#!/usr/bin/env ruby
require 'mumble-ruby'
cli = Mumble::Client.new('IP Address', Port, 'MusicBot', 'Password')
cli.connect
sleep(1)
cli.join_channel(5)
stream = cli.stream_raw_audio('/tmp/mumble.fifo')
stream.volume = 2.7
print 'Press enter to terminate script';
gets
cli.disconnect
Assuming you are on a Unix/Linux system, you can run it in a screen session. (This is a Unix command, not a scripting function.)
If you don't know what screen is, it's basically a "detachable" terminal session. You can open a screen session, run this script, and then detach from that screen session. That detached session will stay alive even after you log off, leaving your script running. (You can re-attach to that screen session later if you want to shut it down manually.)
screen is pretty neat, and every developer on Unix/Linux should be aware of it.
How to do this without reading any docs:
open a terminal session on the server that will run the script
run screen - you will now be in a new shell prompt in a new screen session
run your script
type ctrl-a then d (without ctrl; the "d" is for "detach") to detach from the screen (but still leave it running)
Now you're back in your first shell. Your script is still alive in your screen session. You can disconnect and the screen session will keep on trucking.
Do you want to get back into that screen and shut the app down manually? Easy! Run screen -r (for "reattach"). To kill the screen session, just reattach and exit the shell.
You can have multiple screen sessions running concurrently, too. (If there is more than one screen running, you'll need to provide an argument to screen -r.)
Check out some screen docs!
Here's a screen howto. Search "gnu screen howto" for many more.
Lots of ways to skin this cat... :)
My thought was to take your script (call it foo) and remove the last 3 lines. In your /etc/rc.d/rc.local file (NOTE: this applies to Ubuntu and Fedora, not sure what you're running - but it has something similar) you'd add nohup /path_to_foo/foo 2>&1 > /dev/null& to the end of the file so that it runs in the background. You can also run that command right at a terminal if you just want to run it and have it running. You have to make sure that foo is made executable with chmod +x /path_to_foo/foo.
Use an infinite loop. Try:
while running do
sleep(3600)
end
You can use exit to terminate when you need to. This will run the loop once an hour so it doesnt eat up processing time. An infinite loop before your disconnect method will prevent it from being called until the server shuts down.
When I execute a file with node.js (by typing "node example.js", maybe a http server), then, when I close the Terminal window (Mac OS X Lion), the process is stopped and doesn't answers on requests anymore. The same happens if I type "ctrl c" or "ctrl z". How can I close the Terminal without stopping the process, so my server continues answering on requests.
Use a combination of the nohup prefix command (to keep the process from being killed when the terminal closes) and the & suffix (to run the process in the background so it doesn't tie up the terminal):
nohup node example.js &
You should also look into forever or similar tools that will also automatically restart the server if it crashes, and nodemon which will automatically restart it when you change the code.