Jump to content

Is it possible to run a Node.js script continuously without having an active browser tab open?


Recommended Posts

Hello everybody, this is my first attempt at using the forums so please correct me if this is the wrong place to post topics like mine! Thanks!

 

My account is on the Tommy server and I have set up Node.js following the tutorial on the wiki. Basically, I would like to run a Node.js script continuously without having to keep a browser tab open. I am aware that running scripts for a long amount of time would cause high server load, so I'm just curious if there is a way to run a program without turning on my computer. Thank you in advance for all answers!  :)

Link to comment
Share on other sites

You'll need to use the start.py and stop.py scripts from this tutorial - https://wiki.helionet.org/tutorials/discord-bot - and update them according to your file names and paths.

That doesn't work for me. I have followed the tutorial precisely but I can't get it to work. What can I do?

 

More details: When I execute the start.py script, it doesn't output anything so I assume it couldn't find the file. However, when I open the stop.py, it says "Bot already stopped". I have set up Node.js following this tutorial. Lastly, I doubt there is a problem with my Node.js application because I wrote it in Visual Studio Code and it worked perfectly fine there...

Link to comment
Share on other sites

subprocess.Popen("/home/lookyweb/message.js")
That file isn't executable because it doesn't have a shebang so that won't do anything. Try this

subprocess.Popen("/usr/bin/node /home/lookyweb/message.js")
Also

if 'message.js'.encode('utf-8') in line:
this won't do anything either because the executable will be "node" and the arguement passed to the node executable will be "message.js". You can't kill a process based on its argument like that. So try this

if 'node'.encode('utf-8') in line:
  • Like 1
Link to comment
Share on other sites

 

subprocess.Popen("/home/lookyweb/message.js")
That file isn't executable because it doesn't have a shebang so that won't do anything. Try this

subprocess.Popen("/usr/bin/node /home/lookyweb/message.js")
Also

if 'message.js'.encode('utf-8') in line:
this won't do anything either because the executable will be "node" and the arguement passed to the node executable will be "message.js". You can't kill a process based on its argument like that. So try this

if 'node'.encode('utf-8') in line:

Thank you, I will try that!

Link to comment
Share on other sites

subprocess.Popen("/home/lookyweb/message.js")
That file isn't executable because it doesn't have a shebang so that won't do anything. Try this

subprocess.Popen("/usr/bin/node /home/lookyweb/message.js")
Also

if 'message.js'.encode('utf-8') in line:
this won't do anything either because the executable will be "node" and the arguement passed to the node executable will be "message.js". You can't kill a process based on its argument like that. So try this

if 'node'.encode('utf-8') in line:

Oh, no! That doesn't seem to work either... Here is the structure of my project:

 

home/lookyweb

    |---node

    |   |---node-modules

    |   |    |---...

    |   |---message.js

    |   |---package-lock.json

    |

    |---public_html

        |---...

        |---cgi-bin

            |---start.py

            |---stop.py

 

start.py

#!/usr/bin/python3.7

import os, subprocess, signal

print("Content-Type: text/html\n\n")

counter = 0
p = subprocess.Popen(['ps', '-u', 'lookyweb'], stdout=subprocess.PIPE)

out, err = p.communicate()
for line in out.splitlines():
  if 'node'.encode('utf-8') in line:

    counter += 1
    print("Application already running.")

if counter == 0:
  subprocess.Popen("/usr/bin/node /home/lookyweb/node/message.js")
  
  print("Application started!")

stop.py

#!/usr/bin/python3.7

import os, subprocess, signal

print("Content-Type: text/html\n\n")

counter = 0
p = subprocess.Popen(['ps', '-u', 'lookyweb'], stdout=subprocess.PIPE)


out, err = p.communicate()
for line in out.splitlines():
  if 'node'.encode('utf-8') in line:


    counter += 1
    pid = int(line.split(None, 1)[0])
    print("Stopping application.")
    os.kill(pid, signal.SIGTERM)

if counter == 0:
  print("Already stopped.")

I can't figure out whats wrong but I feel like it's has something to do with this line in start.py:

subprocess.Popen("/usr/bin/node /home/lookyweb/node/message.js")

Please help me! Thank you!

Link to comment
Share on other sites

You say this

subprocess.Popen("/usr/bin/node /home/lookyweb/node/message.js")
but when I look at the file I see this

subprocess.Popen("/lookyweb/bin/node/home/lookyweb/node/message.js")
The directory /lookyweb/ definitely doesn't exist so that will never work. The other thing I forgot to mention is if you open a command with an argument like that with popen you also have to add shell=True to the command. So now I edited it to look like this:

subprocess.Popen("/usr/bin/node /home/lookyweb/node/message.js", shell=True)
It looks like it's working. Go ahead and test it out, and if you leave it running please be sure to check your account load page frequently for the first few days.
  • Like 1
Link to comment
Share on other sites

Thank you so much Krydos, everything works perfectly fine! :) One last question: Can I run multiple Node.js scripts on HelioHost at the same time? If yes, how? Do I need to create different folders in my node folder? (I think that account load wouldn't be a problem because I've ran message.js for a long time and my account load was almost zero.)

Link to comment
Share on other sites

Of course you can. I would just make new start and stop cgi scripts for the other node.js app. I'm actually not sure how you would differentiate between the node.js processes with stop.py though. You'd probably just have to have start scripts for each node app, and then one stop that would stop all of them. You could put the new node.js files in their own directory, or if they share some dependencies it might save disk space to have them use the same node_modules directory. Your load looks fine so far.

  • Like 1
Link to comment
Share on other sites

Hello all,

 

I've got my sample Node.js application working (thanks Krydos). I wrote another one which works perfectly fine on my computer but when I start it on HelioHost, it doesn't work. I'm sure the application starts because I see the console.log() messages in my browser but it doesn't function as expected. The previous app had similar mechanics and it worked... The new app is called test.js. Please help me get it working. Maybe there is a problem with permissions...?

 

Thanks

Link to comment
Share on other sites

I am using the same python script that worked for the previous app. The only thing I changed was the path to my Node.js file.

 

I am making connections to an external platform using its API. I’m sure my code works partially because initializing a session with the API works but — for example — I can’t retrieve any data with it. The problem only appears on HelioHost; not on my computer or on Repl.it.

 

P.S.: I don’t know what passenger is. An explanation would be appreciated! :D

Link to comment
Share on other sites

 

 

I’m sure my code works partially because initializing a session with the API works but — for example — I can’t retrieve any data with it. 

 

Does the API in question happen to use nonstandard ports to communicate? 

 

Sorry, I have no idea what a non standard port is... wait, let me do some research!

 

Edit: Okay, now that I know what a non standard port is, it's time to dig into the code of the library I'm using! Okay, the library uses port 443. I'm pretty sure that's the port for encrypted HTTPS traffic.

 

Edit: I have also noticed that sometimes I have to start my application multiple times because it seems to kill itself instantly after startup.

 

Edit: Can someone please explain what ports have to do with this? I had a similar application up and running on HelioHost using the same library, the same API. It worked.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...