0

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.

codeinprogress
  • 3,193
  • 7
  • 43
  • 69
  • did you try with the absolute path which is: /usr/bin/node. – Pacifist Sep 20 '19 at 05:59
  • Yes I did. I used the /usr/bin/node path but it still wont print out the log. – codeinprogress Sep 20 '19 at 06:04
  • 1
    Can you try making a script and add that in cron. Like create a .sh file and add /usr/bin/node /home/ubuntu/my-crm-app/test.js in that file. Then just call that script from cron. */5 * * * * /path/to/script.sh – Pacifist Sep 20 '19 at 06:07
  • Ok, will try. So where should the .sh file be saved? On the root directory? or inside my home/ubuntu? where? – codeinprogress Sep 20 '19 at 06:08
  • 1
    Put it in your home directory and while calling give the whole absolute path. – Pacifist Sep 20 '19 at 06:09
  • No its not calling it. I created a .sh file /home/ubuntu/cronjob.sh and pasted the /usr/bin/node /home/ubuntu/my-crm-app/test.js there, then I changed my cron job to */1 * * * * /home/ubuntu/cronjob.sh but I still do not see the console.log – codeinprogress Sep 20 '19 at 06:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199713/discussion-between-pacifist-and-codeinprogress). – Pacifist Sep 20 '19 at 06:28

2 Answers2

1

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
root
  • 5,528
  • 1
  • 7
  • 15
1

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.

Pacifist
  • 3,025
  • 2
  • 12
  • 20