1

I use FreeBSD UNIX and its rc system of startup scripts to make my Node.js servers start at reboot as a daemon and start, as a daemon again, and stop them with some commands the system gives me after I logged in to my FreeBSD UNIX user account.

I have read the Handbook related sections and an article on it, and I have written the following script, and the config, in /etc/rc.d/my_script and /etc.rc.conf, respectively. Which make it work as I described. When I add another script for another service only one of them work, I think usually the second one. Each server coded to listen to different port numbers.

my_script_1 is executable for all as '-rwxr-xr-x'.

/etc/rc.d/my_script_1:

#!/bin/sh

. /etc/rc.subr

name=my_service_1
rcvar=${name}_enable

project_dir="/usr/home/ec2-user/dev/projects/my_project_1"

command="/usr/local/bin/node"
command_args="${project_dir}/index.js"

load_rc_config $name
run_rc_command "$1"

/etc.rc.conf includes enabling var set:

my_service_1_enable="YES"

When I add another script and rc.conf entry like those with just name changed to a different name like, my_service_2, only one of them works. my_script_2 is executable as well.

What am I missing? How can I make them work both?

sçuçu
  • 111

1 Answers1

0

When you write "usually" it sounds like a race condition. It is then random which of the instances get to survive.

There is no basic problem in what you are trying to do with the rc scripts. The rc scripts only start the commands. The things you try to start does however need to be able to run in multiple instances. The rc system does nothing (as such) magically to do that.

From what you describe I would guess that parts of project 1 & 2 are using the same resources so node.js can only run one instance. An example would be that both projects bind to the same port. Two instances cannot serve on the same port - so only the first to start (the race) gets to live.

To debug this - then start by ensuring that you handle multiple instances properly. Try disabling your startup scripts. Then start both projects manually at the same time.

First you run:

/usr/local/bin/node /usr/home/ec2-user/dev/projects/my_project_1/index.js

And then:

/usr/local/bin/node /usr/home/ec2-user/dev/projects/my_project_2/index.js

As node outputs to STDOUT and errors goes to STDERR it is easier to see what goes wrong. I am pretty sure that your problem lies around here and that the output can help you further your investigation.

If the above actually works (contrary to my suspicion) then you need to get the log output from the starting node processes in your startup script. It does not seem like node.js logs to a file or syslog. This means that if you try to start the rc script manually you should get error output to the screen. So try controlling your rc script using:

service my_service_1 start

To look further into logging have a look at where is the nodejs-log-file.

NOTE: You are playing with your own scripts in /etc/rc.d/your_script which is intended for the base system. You should put your scripts in /usr/local/etc/rc.d/your_script. The service command searches both locations so it will work the same. No changes nedded to /etc/rc.conf either.

  • The projects are using different port numbers. I am adding this ti question text as well. What else can be sharing same resources for a http server and a web socket server? – sçuçu Oct 20 '18 at 07:56
  • Do not guess or speculate. Get the log info. That is the key. – Claus Andersen Oct 20 '18 at 08:01
  • You are right. I will get it asap. My access to the server is very limited now. I will need to simukate it on a virtualization means. Tried but currently a some libd.so.1... library nide.js lacks. I will update virtual os and tools and reflect. – sçuçu Oct 20 '18 at 08:04
  • Since I have not access the actual server, I have set up myself a FreeBSD on VirtualBox. I have created two node.js projects in seperate folders. But I do not know how to run two nodes now since there is only one terminal tab, it is I think a real terminal, not like the one on my mac, a Terminal emulator, where I can open two tabs and connect to my remote FreeBSD UNIX via ssh two times, and issue different node commands. How do I do that here, other than trying to connect the virtual freebsd by ssh from my mac's terminal emulator. – sçuçu Oct 22 '18 at 16:30
  • Also, is daemon utility an option here? – sçuçu Oct 22 '18 at 19:40
  • I have installed node.js by pkg, and have successfully invoked daemon node project1/index.js and daemon node project2/index.js. Although ps does not show them running, sockstat outputs the port 3000 and 4000, ports the project1 and project2 coded to use, as being used by a command named node. But do not know how to reach them from my host mac (don't know the ip of my virtual machine freebsd). I tried daemon since I cannot run two node commands to test what you said without closing one first since it is attached to stdout. – sçuçu Oct 22 '18 at 19:48
  • I am now looking into 1. being able to run more than one commands from a freebsd terminal while the previous is being run uninterrupted. 2. ssh into my virtual machine freebsd from its host mac. 3. (similar to 2.) sending http requests to a server on my virtual machine freebsd from its host mac, e.g. from a browser. – sçuçu Oct 22 '18 at 19:52
  • I think daemon will enhance the scope and then make it harder for you to debug. You are on the right path by ssh'ing into the VM and browse from your host.You have a lot of lessons to learn - but keep up the work! – Claus Andersen Oct 23 '18 at 07:36
  • Thanks a lot. I hope I will. But what about the 1.. The terminal on my bare bones os seems not enabling to do this, if it was on my mac terminal I could just open another terminal emulator tab and run the other node.js command there. Is there a way to do that on a bare bones freebsd unix deafult shell. Another thing; I have installed curl from packages, via pkg, on virtual os and I have seen the successful response from on virtual os by invoking it with reapective http urls of my projects. – sçuçu Oct 23 '18 at 07:56
  • Probably what I need in 1. is a concept named like shell buffers if it is exists. – sçuçu Oct 23 '18 at 08:36
  • 1
    If you want to use the console you can switch using Alt-Fx - if using ssh - either start multiple sessions - or learn a tool such as screen or even better: tmux. Spend some time learning your shell as well as ampersand can come in handy. SSH, tmux and shell are generic Unix concepts - so investing time in these will pay huge dividends – Claus Andersen Oct 23 '18 at 09:13
  • Thank you very much for these leads. I will check all of them. – sçuçu Oct 23 '18 at 09:20