Not able to use Node.js and Crontab - node.js

I know that you can run a Node.js script in Crontab by doing something like:
0 * * * * node /path/to/your/script.js
But I want to run a Node.js app, not a script, using Crontab. I created a Node.js app in order to write some automated tests using Mocha, Chai and Selenium, and I want to run it periodically by using Crontab. How would I go about doing this? I currently run my app by writing in the command line:
npm run api-pro
Where api-pro is a script from my package.json that invokes some tests for the production api.
Note that if I simply try to write a Crontab job with the command "npm run api-pro" it doesn't recognize the command npm (and obviously I do have Node installed in my computer).

My guess is that the user cron use do not configure the PATH in the same way as your user, and do not know node nor npm.
What you can try is to use the command which node to know where your node binary is (/some/path/to/node)
Then you can use the absolute path in your crontab:
0 * * * * /some/path/to/node /path/to/your/script.js
EDIT:
The difference between adding node and npm to $PATH and using absolute paths is that absolute path will work for one executable, since Linux will not have to search the PATH.
Adding to the PATH will make Linux recognize node and npm just as in your user. The fact that they are in the same folder do not affect that.

For anyone trying to run with npm, here were my steps to get it working on MacOS 12.3.1, using node/npm installed by brew.
Note, this is inflexible, and the PATH will need to be updated if brew updates its paths again:
Print two things we will need
echo $PATH
// /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:...
which npm
// /opt/homebrew/bin/npm
Open crontab with EDITOR="vim" crontab -e
Add these two lines
PATH={what you copied from step 1}
* * * * * cd {your_project_dir_where_package.json_lives} && {location_of_npm_from_step_1} run start >>/tmp/crontab.log 2>&1
Note: The >>/tmp/crontab.log 2>&1 is to help you debug. You can tail -f /tmp/crontab.log in another terminal to watch for STDOUT or STDERR

I guess that by using crontab, you're running your node app on a Linux machine so why don't you write a simple bash script ?
run_test.bash
#!/bin/bash
cd /path/to/your/app && \
npm run api-pro
then your crontab should look like :
0 * * * * /path/to/your/bash/script/run_test.bash
Of course, your script will have to be executable for your user :
$ chmod u+x run_test.bash

Related

Rasbian on Raspberry Pi trying to run Electron app on startup

I'm trying to run a script on startup on my Raspberry Pi, I need to ultimatly run npm run start after everything has loaded in order to launch my Electron app. If I run this command in the terminal it works just fine, but for hours I've been trying to use a crontab, the RC Local and various other things.
My script doesn't run. I can verify that my rc local file is indeed running and my sh file is running, but Electron is never launched, what am I doing wrong?
I've got a command to execute a script that runs in rc.local which is:
sh '/home/pi/Desktop/cockpit-tv-monitor/start.sh'
The contents of start.sh is:
#!/bin/sh
# start-jammer.sh
# navigate to the home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/Desktop/cockpit-tv-monitor
echo "about to run npm run start" > /tmp/rc_test.txt
npm run start
I am getting the echo, but npm run start seems to never launch my electron app, and this command is essentially an alias which runs electron .
UPDATE 05/05 # 09:58
I've outputted the contents of npm run start to a simple text file using:
npm run start >> /tmp/start-output.txt > 2>&1
The error I'm getting is:
> cockpit-tv-monitor#1.0.0 start
> electron .
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Failed to get 'appData' path
at App.c._setDefaultAppPaths (electron/js2c/browser_init.js:5:1300)
at Object.<anonymous> (electron/js2c/browser_init.js:197:2129)
at Object../lib/browser/init.ts (electron/js2c/browser_init.js:197:3540)
at __webpack_require__ (electron/js2c/browser_init.js:1:128)
at electron/js2c/browser_init.js:1:1200
at electron/js2c/browser_init.js:1:1267
at NativeModule.compileForInternalLoader (internal/bootstrap/loaders.js:283:7)
at NativeModule.compileForPublicLoader (internal/bootstrap/loaders.js:225:10)
at loadNativeModule (internal/modules/cjs/helpers.js:35:9)
at Module._load (internal/modules/cjs/loader.js:747:15)
/home/pi/Desktop/cockpit-tv-monitor/node_modules/electron/dist/electron exited with signal SIGSEGV
This is even after adding a delay before running npm run start of 30s, still get the error.
did you try running it from
/home/pi/.xinitrc
just create that file, and insert
cd /home/pi/Desktop/cockpit-tv-monitor && npm run start &
reboot and hope the best.
The error seems to be Failed to get 'appData' path, which is probably an OS related thing.
appData is where Electron saves its cookies and other electron runtime related items. This might mean that your OS doesn't have the proper directory that electron can save to.
For linux (e.g. Raspberry Pi), it looks for two things either and environment variable called $XDG_CONFIG_HOME or something with this file ~/.config.
You can output the value of either by
echo $XDG_CONFIG_HOME
or
cat ~/.config
to see if a paths exist.
If neither work you can update your npm start script and pass an environment variable on start like XDG_CONFIG_HOME="~/usr/electron/myapp" && electron . that adds the environment variable before boot with a directory.

Running a global node module binary from crontab

I'm trying to run a node module (npm install -g lungo-cli) from within my crontab.
What I've tried is to call the node binary from crontab:
* * * * * lungo
No luck.
Then I tried with:
* * * * * /usr/local/bin/lungo
Again, no luck.
So finally I did what I thought it was a workaround, calling the node module directly from a javascript file using shelljs.
Javascript:
const shell = require('shelljs')
shell.exec('lungo')
I even tried running it locally!
const lungo = require('lungo-cli/bin/lungo')
const shell = require('shelljs')
const shell.exec('lungo')
Crontab:
* * * * * /usr/local/bin/node $HOME/scripts/lungo.js
And it's the same, it's not working, it seems like crontab is running in a completely different environment than my zsh shell and can't find any binaries other than the OSX specific ones like cat, rm, cd and so on.
I'm using Mac OSX 10.14.1 (Mojave) and zsh as my shell.
How could I accomplish this? I want to be able to call my node cli programs with crontab.
I'm using Oh-my-zsh in my macbook so I've tried many things to get the crontab task runs but finally, my solution was prepending the .zshrc before the command to run.
*/30 * * * * . $HOME/.zshrc; node /path/for/my_script.js
This task runs every 30 minutes and uses .zshrc profile to execute my node command. Don't forget to use the dot before the $HOME var.
By the way, you can run any command attached to your zsh environment.

Starting node js application using grunt on machine start up

I am new to both linux and Node js basically we have developed Node js application in Windows and I need to deploy it on Debian 8 Jessie and I am able to deploy it on linux and for this I need to install npm, node js, grunt cli etc.
And to run my application I just need to type grunt using terminal and application starts.
But the problem I need to start server every time after reboot of system by typing grunt in terminal.
So need solution how can I start my application/server on machine start.
Also let me know how this stuff works!!.
Thanks
as always there is more than one way
rc.local
the prefered way. rc.local will be executed on system startup.
to edit the file use your favourite text-editor (e.g. nano) nano /etc/rc.local and add your script before the last line containing exit 0
/usr/bin/myscript -arg1 -arg2
exit 0
cronjob
if there is also the need for recurring tasks (e.g a daily backup), cronjob could be a good choice to keep things together.
Within your terminal type sudo crontab -e to edit your cronjobs.
there add your command with the #reboot time argument.
#reboot /usr/bin/myscript -arg1 -arg2

Node js commands don't work

The other day I could type commands on Node command prompt just fine then commands stopped, I dont know what happened or what I had changed. So today I tried and I still get the below message
The system cannot find the path specified.
I reinstalled Node and still same problem. I cannot change directory when I run
cd ~
or
ls
but I can check node version just fine.
node -v
newbie
The stated commands are for linux/unix the Windows equivalent are as follows
cd ~ use cd /
and
ls use dir

Executing Newman REST API tests using Jenkins on Linux

I found this answer Integrate Postman test scripts with Jenkins build server (and possibly there are other ones that could answer my question but I just suck at finding them) but I am still running into issues. I am trying to execute the following with newman v3 using jenkins as a way to automate our REST API Testing.
My Execute Shell script looks like this under the Build steps in the jenkins job
#!/bin/bash
pwd
cd ${PROJECT_NAME}/${GIT_SUBFOLDER}
pwd
ls
newman run ${COLLECTION_NAME} -e ${ENVIRONMENT_NAME} -r html,cli,json,junit --insecure
Ignore the pwds and ls as I originally thought I wasn't in the right directory for my collection and environment exports.
The error I keep getting back in jenkins is:
/tmp/hudson1153303836033593524.sh: line 6: newman: command not found
Any help would be greatly appreciated!
Got your problem, Seems that you are trying to execute your project as shell script in jenkins and jenkins cant get newmanHere is the solution, might solve your problemIn your script export path where npm modules get installedLocate node_modules for your system generally its at /usr/local/lib/node_modulesNow add line in your script which will export PATH:
#!/bin/bash
export PATH="<Your node_module Path>:$PATH"
pwd
cd ${PROJECT_NAME}/${GIT_SUBFOLDER}
pwd
ls
newman run ${COLLECTION_NAME} -e ${ENVIRONMENT_NAME} -r html,cli,json,junit --insecure

Resources