Is it possible to replace stdin file with any other file? - node.js

I have a nodejs server which will be started using nohup. So basically it will not have stdin.
I have created daemon for my nodejs server which does all start stop restart status functions.
But I dont want to stop the nodejs server immediately. I want to stop it after certain tasks. So I have to send command to the server's stdin. So that shutdown will be controlled by the server itself. Yes I can do that by creating a API. But its my last option.
Yes my server takes input from stdin and its working well. But using nohup I will not have stdin therefore no way to enter command in that case.
So my question is, if there is a way so that I can change the stdin to other file and my server will take input from it in the same way it does in case of stdin.

If you are in Linux or Mac you can send signals to your server process. In Windows you can too but I don't know the command.
For example if you want to reload your server you can do
kill -s SIGUSR2 processId
And in your server
process.on('SIGUSR2', function() {
console.log('Got SIGUSR2, reloading server...');
});

Related

Will the script on remote server keep running after ssh timeout?

I'm running some script on remote server using ssh. The task is downloading images to remote server. I'm wondering will the script keep running after I log out the ssh session? Why? Could anyone explain in detail?
If you want the script keep running after logout you need to detach it from the terminal and run it in the background:
nohup ./script.sh &
If you close the terminal where you launched a process in, the process will receive SIGHUP and unless it handles it this means the process will get terminated. HUP means hang up, like in a phone call.
The nohup command can be used to start a process and prevent it from SIGHUP signals getting send to it. An alternative would be to use the bash builtin disown, which does basically the same:
./script.sh &
disown %1
Note that the 1 represents the job id. If you running multiple processes in the background you need to specify the correct job id.

Redirecting Terminal Output of Running Process

I am connecting to a server using SSH and running a minecraft server there with
java -jar server.jar
Now, i want to be able to close my SSH session without closing the minecraft server and, if i need to type some commands to the server or read the server output, i want to be able to re-connect with SSH and get the output and input back to my terminal window.
Is this possible? I've read about redirecting the output to a file, but that won't solve my problem :/
I will use screen to detach and reattach the window.
https://www.digitalocean.com/community/articles/how-to-set-up-a-minecraft-server-on-linux

Stop listening for node.js

When I do:
node server.js
Then my terminal (osx) starts to listen for messages like errors and logs from the node app.
How can I start node without listening?
How can I stop listening programtically from within node.js?
If you are using bash shell (the typical default on a Unix-like operating system such as OS X), you can start it up like this:
node server.js 2>&1 >/dev/null
That directs STDERR and STDOUT to /dev/null.
If you also want to throw the process into the background so you can do other things in the shell, add a & at the end:
node server.js 2>&1 >/dev/null &
Just make sure you know how to bring it back to the foreground (fg command) and/or know how to kill it when you no longer want it running.

Run a persistent process via ssh

I'm trying to start a test server via ssh but it always dies once i disconnect from ssh.
Is there a way to start a process (run the server) so it doesn't die upon the end of my ssh session?
As an alternative to nohup, you could run your remote application inside a terminal multiplexor, such as GNU screen or tmux.
Using these tools makes it easy to reconnect to a session from another host, which means that you can kick a long build or download off before you leave work and check on its status when you get home. For instance. I find this particularly useful when doing development work on servers that are very remote (in a different country) with unreliable connectivity between me and them, if the connection drops, I can simply reconnect and carry on without losing any state.
If you're SSHing to a Linux distro that has systemd, you can use systemd-run to launch a process in the background (in systemd's terms, "a transient service"). For example, assuming you want to ping something in the background:
systemd-run --unit=pinger ping 10.8.178.3
The benefit you'll get with systemd over just running a process with nohup is that systemd will track the process and its children, keep logs, remember the exit code and allow you to cleanly kill the process and all its children. Examples:
See the status and the last lines of output:
systemctl status pinger
Stream the output:
journalctl -xfu pinger
Kill:
systemctl kill pinger
Yes; you can use the nohup command to swallow the HUP ("hangup") signal that is sent to your program when you hang up your SSH session.
Alternatively, if you're writing the server yourself, you can code it to register a handler for the HUP signal, and swallow it inside the program (rather than using an external nohup program that does the same).
In addition to the other replies, you could start your test server thru batch (or at) but as Brian answered you should call daemon
And you could pass the -f option to ssh
As an alternative to nohup, screen, et al. you could revise your server to invoke daemon to detach it from the terminal. This is the idiomatic way to write services for linux.
See also daemon(3).

How to know from a bash script if the user abruptly closes ssh session

I have a bash script that acts as the default shell for a user loging in trough ssh.
It provides a menu with several options one of wich is sending a file using netcat.
The netcat of the embedded linux I'm using lacks the -w option, so if the user closes the ssh connection without ever sending the file, the netcat command waits forever.
I need to know if the user abruptly closes the connection so the script can kill the netcat command and exit gracefully.
Things I've tried so far:
Trapping the SIGHUP: it is not issued. The only signal issued i could find is SIGCONT, but I don't think it's reliable and portable.
Playing with the -t option of the read command to detect a closed stdin: this would work if not for a silly bug in the embedded read command (only times out on the first invocation)
Edit:
I'll try to answer the questions in the comments and explain the situation further.
The code I have is:
nc -l -p 7576 > /dev/null 2>> $LOGFILE < $TMP_DIR/$BACKUP_FILE &
wait
I'm ignoring SIGINT and SIGTSTP, but I've tried to trap all the signals and the only one received is SIGCONT.
Reading the bash man page I've found out that the SIGHUP should be sent to both script and netcat and that the SIGCONT is sent to stopped jobs to ensure they receive the SIGHUP.
I guess the wait makes the script count as stopped and so it receives the SIGCONT but at the same time the wait somehow eats up the SIGHUP.
So I've tried changing the wait for a sleep and then both SIGHUP and SIGCONT are received.
The question is: why is the wait blocking the SIGHUP?
Edit 2: Solved
I solved it polling for a closed stdin with the read builtin using the -t option. To work around the bug in the read builtin I spawn it in a new bash (bash -c "read -t 3 dummy").
Does the Parent PiD change? If so you could look up the parent in the process list and make sure the process name is correct.
I have written similar applications. It would be helpful to have more of the code in your shell. I think there may be a way of writing your overall program differently which would address this issue.

Resources