I want to execute a node.js script within my project folder using cron job on my EC2 hosting. I know this question has been asked before many times on this forum and I followed the answers to reach where I am but I am having difficulties getting the result.
At my root level there are two folders: home and usr
My node lives at /usr/bin/node (which node
gives this path)
My node file lives at /home/ubuntu/my-crm-app/test.js
The test.js has just one console.log("this is a test")
- but I will be writing more code later on if this works.
Now if I execute /usr/bin/node /home/ubuntu/my-crm-app/test.js
from anywhere it prints out the log,
In fact even if I do node /home/ubuntu/my-crm-app/test.js
it prints out the log. So this means my paths for the node and project file are correct and working.
I typed crontab -e
in the system and choose vim basic as my editor to write the cron job. It looks something like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * /usr/bin/node /home/ubuntu/my-crm-app/test.js
So I want to execute the log every one minute. I save the vim file and come out of it and wait and wait and nothing happens. From what I understand my cron syntax is correct. So what seems to be the problem? Do I need to give a different path syntax for node and my test.js within the vim file?
Thank you.