Process.daemon vs. running a Ruby script with nohup + & - ruby

I have a Ruby 1.9 script that I want to run as a long-running background process.
It looks like I have a choice between calling Process.daemon inside the script to daemonize it, or I can just run the script in the background using a shell's ampersand and keep it running after I log out of the server by prefixing the command with nohup.
Which way is better?

Process.daemon seems like a more clean and straightforward way, especially if this is something you would ever way to turn into a full-fledged daemon that is started during boot.

Related

Launch subprocess in Ruby and continue execution of script

I want to launch a subprocess in Ruby, concretely an external .exe file (I'm working in Windows environment). I don't want to block the Ruby script, so I want to return to the next line of the script while the subprocess is running. The Ruby script can even finish before the subprocess is done.
Is there any way to do it? I've tried exec(), fork(), spawn(), system()... but cannot make it work properly.
You could use Thread:
Thread.new { <your logic here> }
There are a bunch of helper methods defined on Thread which will allow you to monitor and manipulate the thread.

Ruby run external program stops script

I have a ruby script that midway through I need it to run another program.
After running the program the rest of the script doesnt get run. For example:
# some ruby that gets run
exe = "Something.exe"
system(exe)
# some ruby that doesnt run
I have also tried using Open3.popen2e(cmd) and Open3.popen3(cmd) but its the same.
Can anyone help me understand what is happening here and how to fix it?
note: I'm using windows
Try to run Something.exe in a new Thread:
Thread.new { system("Something.exe") }
In case you want to run your System.exe asynchronously and continue without waiting it to be finished, you could use spawn or multithreading.
pid = spawn('System.exe')
Process.detach(pid)
According to this previous answer, this should work on Windows as well (while fork or other methods don't).
In this article you can find several examples using system, exec, fork, spawn and Thread on Unix.
I cannot reproduce it, but it could be worth to see if using system("start System.exe") works on windows like system("cmd &") works on UNIX. You can refer to start documentation here.

automatically running a bash script in the background

I'm interested in finding out how I'm able to do the following, but automatically.
Right now I have a bash script that looks something like this:
#!/bin/sh
sass --watch htdocs/css/scss:htdocs/css --debug-info
Now, because I want to be able to run other command-line tasks at the same time as this script is running, I could just run it inside a new window, but I prefer to have it run as a background process, which I achieve by:
Hitting [ctrl] + [Z]
Running bg
Which lets me continue to use the command-line, while also seeing the output from the sass command.
I'm also able to use jobs to see what's running, and finally, fg to bring the background script to the forefront, and use [ctrl] + [c] to cancel out of it.
All of which is fine, but it's a bit long-winded - is there any way that I can edit the bash script so it will automatically run in the background, similar to what I've described above?
Thank you
$ ./script &
^--- run script in background from the get-go
An alternative to this , which I found useful is to write a function
Foo(){
Do stuff
}
and call the function and send it to the background
Foo &
All within the script, so you don't have to do
script.sh &
Instead, just invoking the script will send it to the background.

how to kill a group of processes in clozure cl?

I want to run a shell command within ccl, but this command may be hung for some reason. So I want to kill all the sub process generated by this command. How can I do this?
I have tried trivial-shell to run the shell command, when the command not hung, it works well.
I also use with-timeout macro which is in trivial-shell to check the timeout, it just give me a timeout-error condition, the shell process is still hunging there. Here I just want to kill them all and return something.
Thank you all.
As far as I can tell, trivial-shell only provides a synchronous shell call so there's no simple way to terminate ongoing subprocesses.
I suggest calling Clozure Common Lisp's implementation-specific ccl:run-program function with :wait nil to run the jobs asynchronously. You can then call ccl:signal-external-process on the running process to kill it if you need. Documentation here.

Programmatically start a series of processes w\ job control

I have a series of 7 processes required to run a complex web app that I develop on. I typically start these processes manually like this:
job &>/tmp/term.tail &
term.tail is a fifo pipe I leave tail running on to see the output of these processes when I need to.
I'd like to find away to start up all the processes within my current shell, but a typical script (shell or ruby) runs w\in it's own shell. Are there any work arounds?
I'm using zsh in iTerm2 on OSX.
You can run commands in the current shell with:
source scriptfile
or
. scriptfile
A side note, your processes will block if they generate much output and there isn't something reading from the pipe (i.e. if the tail dies).

Resources