Schedule a .js file to run - javascript

I am trying to find a solution of how to execute a JavaScript file a couple of times a day (Like Task Scheduler on Windows)
At the moment, I execute the file in VS Code or Command Prompt using node file.js.
Is there any way to automatically execute it?
I have tried the following with Task Scheduler, but is not executing the file, just opening VS Code:
Action: Start a program
Program/script: "C:\Users\AppData\Local\Programs\Microsoft VS Code\Code.exe"
Arguments: -l -i -c "C:\Users\desktop\folder_containing_the_file; node writeToFile.js"

It can't be executed when the computer is off. But you could use the Amazon AWS Free-Tier services to schedule and run it.

Related

K6 how run node command in the setup function

Please excuse my limited K6/Javascript knowledge.
I have been given a javascript file that can be used to create a batch of users to be used within my k6 load test.
The script is currently ran from the commmand line using this command:
node helpers/createUsers.js 10
I want to run this script in the 'setup' function in K6.
Can I run node commands directly in K6 as above, or do I have to turn it into an export function and then call it?
To my knowledge, you cannot run nodeJS or any nodeJS modules directly from a k6 test script. If the script helpers/createUsers.js is using any node modules, then you should follow this. and have the script in the setup() function.

How can I run a JavaScript automatically on a command-line?

I have a simple JavaScript myScript.js with code as below;
var printer = function(someString){
console.log(someString);
}
printer("This is printed on console.");
I'm on Windows 10 and need to be able to call myScript.js on a command prompt and have the code executed without doing node myScript.js. Is there a way to set up things so that Command prompt or PowerShell can automatically call Nodejs or other JavaScript engine?
If your Windows can run normal shell scripts on the command line then you can add a shebang line to your Node script just like you do with shell scripts. For example:
#!/usr/bin/env node
// your node code here
Also you can try to configure Node to be invoked for all files with the .js extension but this can be a security hazard because you may execute arbitrary code just by clocking on JavaScript files that you may have downloaded on your system so I wouldn't really recommend that.
Another alternative is to make a BAT script for you Node app:
example.bat:
node example.js
example.js:
// your Node script
That you will be able to run with just:
example
on your command line.

Laravel task scheduling on localhost

I am new to laravel.I want to schedule some task on my application using laravel 5.3.But I still run my application on localhost.Please can someone explain me to how laravel task scheduling can be done on localhost.
I assume you are using Windows.
Schedule a task to run php artisan schedule:run using the at command.
You also must make sure the php executable is in your system path: How do I add my PHP directory to the PATH on Windows?
How To Use the AT Command to Schedule Tasks
To start working with Laravel Scheduling, first, you have to run this command on the terminal.
php artisan make:command command_name
Then check the files in the directory. App\Consol\Commnads you'll have a command_name file. Edit as given in the following script.
protected $signature = 'save:file';
After that add your own methods and tasks to Handle function
At the end, run this command to check whether the scheduling is working or not.
php artisan save:file

Running two node processes inside a shell script

I am a total beginner in shell scripting.
I have a node js program, and an API, which uses mongoDB. So the API uses express and is linked to my routes and schemas - all I have to do in order to start the API server is run node app.js
However, I also have my main program, which polls specific urls that the user adds through the database, and then saves the data that is returned from the poll request in the database. This happens every 'x' seconds, therefore I always open the mongo connection when the poll happens, and close it as soon as it finishes it. So in order to run this program, I need to run node main.js
The app.js seems to just open the mongo connection once, when the program is run.
So my question is - can I link them together somehow by writing a bash script so I can start both of the processes together and end them when needed?
I tried doing this:
#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING
node misrepo/app.js
node misrepo/main.js
However this only starts the app.js, and does not run my main.js application.
Any help would be appreciated as I am just trying to figure this out from googles help!
Good answer by Markus,
nevertheless, I need a add a little comment and due to lack of reputation, I need to create an answer.
It is better to start both processes in background (caused by the &) and wait for both processes to finish. Otherwise, it seems that both processes are finished while you only wait for the second command. That might cause trouble in processing output of both commands.
#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING
node misrepo/app.js&
node misrepo/main.js&
# wait until both background processes finish
wait
Best,
Frank
Service management is tricky and there are tools built specifically for it. It's best if you don't reinvent the wheel in bash.
Use forever or pm2 to manage a service.
npm install forever -g
forever start misrepo/app.js
Put them in your script
#!/bin/sh
forever start misrepo/app.js
forever start misrepo/main.js
You can also include these commands (or your script) in the scripts section of your package.json
{
"scripts": {
"start": "forever start misrepo/app.js && forever start misrepo/main.js"
}
}
Then npm start will start your app. stop and restart are also standard.
If your doing this on a server, then use the native service manager to manage each service. This is likely to be systemd if you're on linux.
To start two programs in parallel you can do the following:
#!/usr/bin/bash
# declare STRING variable
STRING="Starting node processes"
#print variable on a screen
echo $STRING
node misrepo/app.js & node misrepo/main.js
This will start node with misrepo/app.js and immediately another instance of node with misrepo/main.js.
In your example the script blocks until node misrepo/app.js finishes execution and only then starts node misrepo/main.js
this is maybe too late but hope this will help somebody who is searching for the answer.
#!/bin/bash
//enter in to first folder
cd /var/www/html/demo/
//check the files(if no need remove ls
ls
//this will start first node process and after & enters to the next directory
node app.js & cd /var/www/html/demo1
//to ensure the cd command from before line
ls
//starts second node server
node server.js ;
//executes the bin file
exec bash

Run Windows Node.js script in background when the user logins by setting a Run key

I'm running on Windows Server 2012R2.
I have a node.js script which I want to run when the user performs logins.
To do this I'm setting the command to run in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry path.
I set this command:
/path/to/node/node /path/to/node/script args
This works fine, but it will spawn a terminal showing the command output, while I'd like to run this command in background and detached from any command terminal.
I tried to replace the previous command with:
start /b /path/to/node/node /path/to/node/script args
but in this case there is no evidence that the script was even started.
I also tried to wrap the following command in a .bat script:
start \b node script args
set the name of the script in the Run key: in this case I can see terminal flash but then the script is not running anymore (I suppose that the script is executed but then it is stopped as soon as the parent process is terminated).
I want to avoid to convert the script in a windows service, as long as a simpler solution is possible.
A solution not using the Run registry key is also fine, as long as it fulfils my requirement (run a script in background when the user logins).
Actually it's pretty easy , use forever.js module
after installing the module use
"forever start main.js"
It will start running as your background process

Categories

Resources