Node JS run using Cron Job on AWS - node.js

I am trying to run a node script on an AWS Ubuntu server. When I log into the Ubuntu server from my terminal and run my script with the command "node dacDev.js" it works just fine. The script writes to a log file in another folder. I want to run this with a cron command on AWS, but it won't run. Here is what my cron job says.
"* * * * * /home/ubuntu/.nvm/versions/node/v13.14.0/bin/node /home/ubuntu/getmyteatime/cronjob.sh"
The file cronjob.sh contains the absolute path of the node script. It reads:
node /home/ubuntu/getmyteatime/dacDev.js
Nothing runs. What am I doing wrong?

For the node, you also need an absolute path to run any.js file. This is achievable but rather than running with node try to write a script and do the following:
echo $PATH # /path/to/env
which npm # /path/to/npm
Paste below lines when the command crontab -e is executed.
PATH=/path/to/env
* * * * * cd /path/to/app && path/to/npm run script >>/tmp/crontab.log 2>&1
>>/tmp/crontab.log 2>&1 -- means, it will log the result to this path for debugging purposes.

Related

Shell Script not executing, added to crontab

Here is my shell script, myscript.sh located in ~/bin
cd ../environment
. env/bin/activate
python3 office.py
The script office.py updates the database. I've tested and works with no issue. I used this command ./myscript.sh
Here is cronjob */5 * * * * cd ~/bin/myscript.sh added to crontab -e
When i check database, no changes. The cronjob isn't running? How do i solve?
You are not running the script but just trying to change directories, which will fail as myscript.sh is not a directory. You need to first cd ~/bin as you are using relative paths in your script and then run the script. Use this line:
*/5 * * * * cd ~/bin && ./myscript.sh
Also you may wanna check the syslog to check for cronjobs.
grep CRON /var/log/syslog
Have a look at this thread for more information on logging cronjobs.

running pm2 as part of crontab script

I have a crontab command set up like below
*/2 * * * * cd dir && dir/keepalive.ksh qa >> /var/log/test/keepalive.log
the keepalive.sh script essentially calls a start script with 'test' argument which executes the following script
print "Start the following proccesses"
if [ ${ENV} == "qa" ]
then
dir="my path"
print "Start QA Server"
pm2 start ${dir}/server.js -- app-env=qa db-env=qa
fi
exit
Problem: The pm2 command never starts up the process. I also tried to use the full path of pm2 (/usr/local/bin/pm2) but still no luck. I can see the output of the print statements which means the script is getting executed as expected.
Any idea what might be going wrong?
I ran into the similar problem before and it turned out I need to prepend the pm2 command with the exact path to my node executable like this
/usr/local/bin/node /usr/local/bin/pm2 start myapp.js
So in your case, you can try
/path/to/node /usr/local/bin/pm2 start ${dir}/server.js -- app-env=qa db-env=qa
Where /path/to/node needs to be replaced with the output of which node in your terminal.

Cron not running while the commands are fine, why?

I have the following cron that checks if my Neo4J DB is running and if it's down, it shuts down the Node.Js app, then restarts the db and then starts the app again. The second line is the backup script.
* * * * * /home/noduslabs/neo4j-community-2.0.1/bin/neo4j status || /home/noduslabs/webapps/infranodus/bin/forever stopall && /home/noduslabs/webapps/infranodus/bin/forever start $HOME/webapps/infranodus/infranodus/app.js && /home/noduslabs/neo4j-community-2.0.1/bin/neo4j start
10 14 * * * ~/webapps/infranodus/bin/backup
When I run the code myself from the shell, such as
/home/noduslabs/neo4j-community-2.0.1/bin/neo4j status || /home/noduslabs/webapps/infranodus/bin/forever stopall && /home/noduslabs/webapps/infranodus/bin/forever start $HOME/webapps/infranodus/infranodus/app.js && /home/noduslabs/neo4j-community-2.0.1/bin/neo4j start
It does everything well.
But the cron doesn't do that check every minute and doesn't relaunch anything...
PS When I do crontab -l it lists the above and I edit it using EDITOR=nano crontab -e - just in case that matters...
Any idea why?
Thanks!
The problem was that Neo4J is a Java program and it was not running from cron correctly – not the same environment variables.
What I did was to change cron so that it runs a shell script
* * * * * ~/webapps/infranodus/bin/checkdb
and in side that checkdb script I have:
#!/bin/bash -l
/home/noduslabs/neo4j-community-2.0.1/bin/neo4j start
which simply attempts to restart the DB (if it started already it will simply not start because it's the same process)
So that's a workaround and the first line - to use bash shell is very important - so it stays with the same params.

Downloading files with bash via cron

I have build a bash script that gets .tar.gz files from IMDb and writes to a log file, the script works when run on its own as I can see the folder with the files present, but when I run the script via cron it doesn't work. Would this be due to permissions? I have edited the sudo crontab file, but I'm not sure what else I need to do.
Try this solution:
Cronjob is a file that contains your job:
cat cronjob
* * * * * bash /path/to/script.sh >> /path/to/log.txt
Then you should set executable permission and start cron service:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
After that you should tell cron service to run cronjob file:
crontab cronjob
Your script should download a file.
If your script doesn't run you should run it from good path[full path], so your cronjob file would be something like this:
* * * * * /bin/bash /path/to/script.sh >> /path/to/log.txt

How can you execute a Node.js script via a cron job?

Quite simply, I have node script that I want to execute once a month.
30 6 1 * * node /home/steve/example/script.js
But this doesn't work, presumably because of path or the shell the command is being ran under. I've tried the following means of executing node via cron (tested with -v):
steve#atom:~$ node -v
v0.4.2
steve#atom:~$ sh node -v
sh: Can't open node
steve#atom:~$ bash node -v
/usr/local/bin/node: /usr/local/bin/node: cannot execute binary file
steve#atom:~$ /usr/local/bin/node -v
v0.4.2
steve#atom:~$ sh /usr/local/bin/node -v
/usr/local/bin/node: 1: Syntax error: "(" unexpected
steve#atom:~$ bash /usr/local/bin/node -v
/usr/local/bin/node: /usr/local/bin/node: cannot execute binary file
I've ran out of ideas to try, any advice?
just provide the full path to node /usr/local/bin/node in your cron job like:
30 6 1 * * /usr/local/bin/node /home/steve/example/script.js
These answers here saying using absolute path will all cause major problems for running a larger node app!
Real Complete Solution
Edit Cron Jobs
crontab -e
Find Node Path
which node
CD into the destination folder, then Change Cron Job according to Node Path and run script
*/2 * * * * cd /home/destination/path && /bin/node index.js
This will then allow you to run a full NodeJS application without all the errors like how using an absolute path for your index.js file.
Additionally, just put #!/usr/local/bin/node at the top of the script you want to execute. Then it will automatically know to execute the script with node. Make sure the file is executable as well.
You can also specify paths to binary files on top of your user crontab like:
PATH=/bin:/usr/bin:/usr/local/bin
* * * * * cd your/path && node foo.js
* * * * * cd your/path && npm run bar
in my laptop using Linux mint the given path not working so i used this to get a work around.
$ which node
$ /usr/bin/node
this worked for me.
This also works for user-level cron jobs
*/2 * * * * cd /home/destination/path && $(which node) index.js
I don't know if changing your relative paths in your script to absolute paths is a good idea
(what happens when your file system changes or you deploy in another environment?)
You could try wrapping it in a shell script, setting some environment variables in the crontab execution. (specifically PATH & NODE_PATH for starters)
Try my suggestion for this similar question:
https://stackoverflow.com/a/27823675/608269
NVM users:
Save the following script (say, cronjob.env.sh) anywhere at your PATH (let's say: $HOME/bin). Remember to make it executable (chmod +x). Replace 'username' with your user name as well:
#!/bin/bash
export NVM_DIR="/home/username/.nvm" #replace "username" with your user name
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Edit cronjob:
crontab -e
Insert/edit your job:
0 0 * * * (. ~/bin/cronjob.env.sh; ~/bin/my-script-with-node-cmd.sh)
Calling node command (instead of some shell script), like the below job, works as well:
*/1 * * * * (. ~/bin/cronjob.env.sh; node ~/index.js)
PM2 users:
A different approach is using pm2, with --cron option, which accepts cron patterns (inside quotation marks):
pm2 start index.js --no-autorestart --cron "0 0 * * *"
--no-autorestart is for one-time scripts (otherwise, it will be restarted every time it is completed).
Use absolute paths for the node alias and the file to be run.
Edit Cron Jobs
crontab -e
Entry to Run Our Node File
This will run every minute.
*/1 * * * * * /bin/node /public/test.js
Full Tutorial
https://askmacgyver.com/blog/tutorial/how-to-run-node-scripts-from-a-cron-job
If you want to preserve the nvm functionality and allow your code to get an updated node version without needing to change the cron job info, put your job in a shell script that first sets up nvm, switches to proper node version, then runs the actual job. Here is an example for a project called invoicing that includes a cron script in its package.json.
File invoicing.sh:
#!/bin/bash
cd /home/produser/invoicing
export NVM_DIR="/home/produser/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
npm run cron
Then set this up as the command in your cron job listing:
0 16 1 * * produser /home/produser/invoicing/invoicing.sh
Now, if you change the node version in your project and update your .nvmrc with the proper version number and pull the new code onto the server, the next time the cron job runs it will run with version of node specified. Note that you must also make sure the server has the required node version.

Resources