codeinprogress
codeinprogress

Reputation: 3501

EC2 cron job not executing my Node.js script

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.

Upvotes: 1

Views: 1089

Answers (2)

Pacifist
Pacifist

Reputation: 3201

This is something you can achieve by creating a script and calling it through cron.

Just create a script named cronjob.sh:

#!/bin/bash
/usr/bin/node /home/ubuntu/my-crm-app/test.js >> /var/log/my-crm-app`date`.log

Above command will run your node program and will generate the logs. You can anytime refer to these logs to see the execution of command and for any errors if any.

Your cron will look like this then :

*/1 * * * * /path/to/script/cronjob.sh

Just give permission to this file chmod +x /path/to/script/cronjob.sh

As this command will be running every minute and creating logs, just take care of removing these logs file after a certain period of time to avoid high disk utilization.

find /path/to/logs  -name "<filename>*.log" -mtime +30 | xargs rm -f

Above command will find all the logs starting with the filename and more than of 30 day age and will delete it. Just add this line in your script and logs will be taken care.

Upvotes: 1

root
root

Reputation: 6068

Quoting this:

Cron doesn't run commands using a terminal you opened. It runs jobs in the background

If you really want to see the output, see this answer.

But that is not what you want. You want to redirect the output of your background job to some log file:

*/1 * * * * /usr/bin/node /home/ubuntu/my-crm-app/my_launcher.sh

where my_luncher.sh does

#!/bin/bash
/usr/bin/node /home/ubuntu/my-crm-app/test.js &> /var/log/my-crm-app.log

Upvotes: 1

Related Questions