How to close a Terminal window while executing a file with node.js without stopping the process? - macos

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.

Related

Quit Ubuntu Application by "clicking" the x-Button via Terminal

I am trying to quit a program with a bash script. The problem is that it has to be quit via the the x-button to save some settings. So pkill program do not work in my case. I know that I have to sent a signal for the x-button but I can not find witch one.

What does "program &" mean on the command line?

I need to develop a client and server program with using sockets. My program should get port number from the command line. I saw an example which says "myprogram 2454 &".
I wonder what that & (ampersand) means there.
It means to start the process in the background. http://tldp.org/LDP/abs/html/x9644.html so that you may continue to use your shell session to run other programs. You can then use fg to "foreground" your process again.
The ampersand (&) means that you want to run myprogram in background. This is normally used when you want to stay on your command-prompt and continue the work on the same session.
Example
somescript &
will run the somescript shell script in background. You will get the prompt back on the next line. If you run somescript without & then the prompt may not appear back because somescript may take more time.
The best way is to run it in background with no hangups, in which case even of you loose your connection to the host the process keeps running on the UNIX or Linux host.
For example
nohup somescript &
the jobs command will display the jobs running in background.

bash ignores & for last command in loop

I just wrote my first bash script to start some redis instances on a development server. While it is mostly working, the last opened redis instance is blocking the active terminal – though I have the trailing & sign and the other started instances aren't blocking the terminal. How would I push them all to the background?
Here's the script:
#!/bin/bash
REDIS=(6379 6380 6381 6382 6383 6390 6391 6392 6393)
for i in "${REDIS[#]}"
do
:
redis-server --port $i &
done
It sounds like your terminal is not actually blocked, your prompt just got overwritten. It's a purely cosmetic issue. Due to the way terminals work, bash doesn't know to redraw it so it looks like the command is in the foreground.
Run the script again, and blindly type lsEnter. You'll probably see that the shell responds as normal, even though you can't see the prompt.
You can alternatively just hit Enter to get bash to redraw the prompt.

Get bg jobs to fg after closing terminal window/session

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.

Stop derby server hanging onto the terminal window

Im running derby server from the command line on ubuntu. Problem is when i start it, it stays running in the terminal window, so it print all its logs straight to terminal. When i close the terminal window, the server shuts down. Is there anyway i can start derby from the command line, and then be able to close the terminal without shutting down the server?
You can suspend it with CTRL-z and run it in the background with bg and then disown %1 (or substitute 1 with the job number shown between the brackets in the suspend message).
Example...
$ bundle exec script/rails s
[1] + 14192 suspended bundle exec script/rails s
$ bg
[1] + 14192 continued bundle exec script/rails s
$ disown %1
$ exit
Another technique is to use your window manager. Set up an application menu item that starts the network server for you, with the various output redirected to files. Then start your server by choosing that menu item.

Resources