NodeJs - can it reboot its host pc? - node.js

Have a nodejs - socketio application kiosk running windows 7 in kiosk mode(desktop).
Occasionally on boot a handshake doesn't occur between socketio and the client app also running on the same pc. There is a connection but it is immediately dropped. This happens randomly roughly 5% of the time on reboot
The only way I've been able to reliably get the handshake working is to manually reboot the pc.
I can see the nodejs trace showing the lost connection when the error occurs.
I was wondering is there was a way for nodejs to dynamically reboot its host pc (in this case running Windows 7)?

Windows has the shutdown command: How do I shutdown - restart - logoff Windows via a bat file?
You should be able to spawn an process to execute that.

Related

How to shut down linux from a web service

Use case is web service intended to run as an appliance on a headless Raspberry Pi. OS: Raspbian (Debian).
The web service is running under a non-root service account, using a custom-rolled Boost Beast-based web server (avoid!).
What I want to do: provide a button in the web interface, running in a remote user's browser that allows the system to be shut down or rebooted.
What I've tried so far ... (C/C++)
system("/usr/sbin/shutdown -P now"); // (or -r for reboot)
Fails with the messages:
... [3460]: Failed to set wall message, ignoring: Interactive authentication required.
... [3460]: Failed to power off system via logind: Interactive authentication required.
... [3460]: Failed to open initctl fifo: Permission denied
... [3460]: Failed to talk to init daemon.
I also tried creating a wrapper executable that has been SUID-ed to root, that in turn calls system("/usr/sbin/shutdown ...") with identical results. So even with root credentials, shutdown fails. (Also tried /sbin/reboot, which also fails).
The service in question is written in c++, and services a web socket. The web client app sends a web socket request to shut down the system; and the shutdown is performed in the C++ server code. Everything works fine when the server is started in an interactive session; but it does not work when running in a systemd service.
The web server itself is running as a systemd service using a system account (so no login credentials, and I really do not want login credentials for the service account).
I'm perfectly willing to reconfigure the system, or create a service specifically for this purpose if necessary, and to do what I can to address security issues. But I have no clue as to what the path forward is. The issue is particularly pressing because there's no shutdown button on an out-of-the-box Raspberry Pi (although I do plan to address that for my own Pi). The fact that one CAN install a shutdown button on a Raspberry Pi means there must be some what to do it.
Ideally, I'd like to leave the R-Pi in a state that's usable for other purposes. So a no-password boot into a shell isn't an attractive option. I don't think. And rsh-based solutions are unattractive because they would require the appliance to have a preinstalled login account, with pre-installed credentials (and we all know how THAT goes).
I solved the same issue by creating a C++ daemon, running as root, and listening ONLY on local host TCP port, in charge to shutdown the system when contacted by the web service.
Edit: Use https://man7.org/linux/man-pages/man2/reboot.2.html to shutdown the OS.

Running GUI without need of remote desktop connection on-going

Build: Server with Ubuntu 18.04 in data center with Ubuntu Mate Desktop interface.
As I understand, any GUI app needs a display in which to function. I connect to the server via x2go to display the ubuntu mate interface.
Once connected, I have a scheduled cron job that launches a terminal, a GUI and some commands.
Everything works perfect as I have previously detected what display I'm using as a user and specify that to launch the terminal and the commands.
Problem: if I'm not connected via x2go client to the server and provide that display, I noticed today that the cron job didn't launch at the specific time (08:50), which was previous to my x2go client connection (09:23); it just launched when I did the x2go client connection from my desktop manually -- my understanding is that it didn't launch because there was not any available display.
I'm not technical enough to get more deep into this problem.
¿Is there anyway in which I can make the cron script function (i.e. that it launchs the terminal and the GUI) without the need of manually getting into my desktop and launching the remote desktop client (x2go)?
My ideas go for having another minimal setup in that data center or another that takes the advantages of being there and have a physical monitor to display constantly connected to the main server OR just manually entering the server via x2go every day.
EDIT: When I disconnect the x2go client session from the server, the launched GUI and etc perfectly maintains, without dissapearing because "I have terminated the session and eliminated the display".
Finally got it working with the great help of one of x2go founders (Oleksandr Shneyder) after understanding how x2go handles the sessions and the X server.
Sessions can suspended/hibernated, disconnected and terminated.
Suspended/hibernated: when "manually" or automatically the x2go server side suspends so that it saves resources.
Disconnected: when you exit the session window and the x2go client in your desktop/laptop.
Terminated: when you log out from the DE environment or issue the x2goterminate-session command in the terminal inside the session.
My x2go app was somehow entering into suspending mode in the server, causing that the display (50 in my case) in which the scheduled cronjob need to be run and launche the GUIs, "was not awaken" until I entered into the session via the
app. In that moment, the cronjob started executing.
To tell x2go server to not hibernate, I needed to change the X2GO_NXOPTIONS in /etc/x2go/x2goagent.options to X2GO_NXOPTIONS="sleep=0"
After this, everything functions perfectly in the server.
Hope it helps others.

Remote login from Web application using Node.js

I have a remote machine that runs Ubuntu 14.04. It is powered on, but not logged into. I am writing a web application using Node.js that takes a username and password and logs in to this machine. This application is supposed to run on an Android tablet. I am not going to send further commands through the terminal, so I don't really require the session, but just logging in so that the remote machine starts running some processes.
I'm having trouble on how to start coding this. I'm not sure about what Node module to use, and whether or not to use the SSH protocol.
I've gone through a couple of solutions to related questions on the internet, but they either don't work on Android, or need the remote machine to be running some server already(which is not the case here, as the machine is not logged into).

CentOS 6 VNC Server denies connection

I have set up tigervnc-server on my CentOS 6 machine following this guide. At first everything worked fine, I could connect to the server using TightVNC Viewer on my Windows machine. I left the session running in the background for a while. The server closed the inactive connection, which it also does for ssh or any other connections, so that's still normal. So I reconnected and everything was fine.
Then I decided to add a different user to the VNC users, as I was running my first connections from root. That's where it all broke. I couldn't connect to he server anymore, it always told me that the remote computer denied the connection. I made sure the server ports were open in iptables, tried to reinstall tigervnc-server, changing the config back to how it was before it broke and disabling the Windows Firewall. After reinstalling it, I was able to launch it without setting a vnc password, so it either saved it somehow from before or something broke entirely. None of my attempted fixes worked.
Turned out to be me not working on linux for over a year and I finally figured out I had to start the vnc server from the user I wanted it to run on.

Share D-BUS message bus on Linux when launching applications remotely

I'm trying to launch two applications in my server using SSH remote connection. These applications are using D-BUS message bus for communication.
If I login to my server computer locally, the communication between these applications works perfectly. However when I try to launch these applications remotely, errors related to D-BUS occurs.
Is there any way to share D-BUS message bus when starting applications remotely on server computer?
My server is running on Ubuntu 12.04.
Finally found a solution to my question.
First I needed to start ssh connection with trusted X11 forwarding enabled:
ssh -2 -Y user#address
In this remote connection I started the application first application with new D-Bus: dbus-launch --autolaunch=machineID app1. Then I opened new SSH connection and also started second application together with D-Bus with the command dbus-launch --autolaunch=machineID app2. MachineID can be found from ~/.dbus/session-bus/ in Linux environment.
That is how I was able to make this two applications to use the same D-Bus message bus in remote connection.

Resources