EC2 cron job not executing my Node.js script - node.js

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.

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.

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

Related

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

BASH - Most efficient method of setting this script as a timed/routine event?

I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.
Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
Could you provide some sort of an idea into the syntax, if you can, as well.
This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.
Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.
cron entries have six fields-:
mins hours day month day-of-week path_to_command
0-59 0-23 1-31 1-12 0-6 command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.
cron entry to run the script every hour for all days and months-:
0 * * * * /path/change_wallpaper.sh
to list your current cron jobs, type
crontab -l
Edit your cron jobs and add the new cron entry-:
crontab -e
Check the new setting is in place -:
crontab -l
I would personally run the script using following crontab:
0 * * * * $HOME/changewallpaper.sh
which you can install as a user with this command
crontab -e
Other solutions include running daemon script from file ~/.xprofile
For more information please refer to
man crontab
man 5 crontab
Also check out this project Variety.
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
That would be a daemon. And there's no need to write your own. It's a bit tedious in bash if you want pidfile, start|stop|restart etc. Just add a new cronjob which'll execute your script every n minute or something.
Edit your cronjobs
crontab -e
Execute script every 30 min: (not the same as 30, which would do it every hh:30!)
*/30 * * * * /path/to/your/script
Restart cron. How depends on distro, here's Ubuntu:
service cron restart
List cronjobs:
crontab -l

Where to locate Centos 6 cron job .sh file

I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.

Confusion about Crons

this is my first time writing script for cron job.
I wrote my code in shell, (which it works) and I'm trying to set it up for cron.
So here is my question. How do I set up the cron? Am I suppose to write
10 * * * * /home/workstation/deleter.sh (I want it to run every 10min)
right underneath #!/bin/sh? How would I execute it? (deleter.sh has permission via chmod)
man 1 crontab returns "No entry for crontab in section 1 of the manual"
I'm really lost and confused right now. If someone know how to set up cron please tell me!!
Thanks in advance
#!/bin/sh
counter=0
logloc=/home/ServerLogs
backup=/home/test
## Reads the location of the file systems that needs to be investigated from location.txt
## and save it into an array
while read -r line; do
Unix_Array[${counter}]=$line;
let counter=counter+1;
done < location.txt
## Reads Email recipients and save it into an array
More code continues from here......
The following will open your environment's text editor and load the crontab:
crontab -e
Your crontab entry is mostly correct. In order for your script to run every ten minutes it should be changed to:
*/10 * * * * /home/workstation/deleter.sh
The entry you indicated would run the script at the 10th minute of every hour.
To setup the cron, you can do one of two (main) things. The first would be to place the specified line in /etc/crontab. The second would be to run crontab -e and place the line in there. I would recommend to use crontab -e so the cron will execute as your own user account.
If the full path to the script is /home/workstation/deleter.sh and it does have execute-privileges, as you specified - your current line will have it execute 10-minutes past the hour, every hour. To get it to execute every 10 minutes, you'll have to use */10, like this:
*/10 * * * * /home/workstation/deleter.sh
this might help
http://www.manpagez.com/man/5/crontab/
you need to get an entry into your crontab
One of the best links I came across when I first learned about cron! Bookmark it
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

CRON : Scheduling task

I am using ubuntu 12.04.I am using a script(./home/sam/code/imageUpdate) to synchronising images from server to a particular folder in local system. And I have to run the script in the evening always. So I want to write a crontab which will automatically runs the script.
My commands :
$crontab -e;
And added the scheduled time to the crontab file.
# 50 17 * * * cd /home/sam
# 52 17 * * * ./code/imageUpdate > image1.txt
Then I saved the file and waited for the result.
But I didn't get any result. No image was been synchronised to image1.txt file.
Have I left any step ?
Please help me out...
Thanks in advance.
Make sure you don't have hashes (comments) at the start of your crontab commands.
Additionally:
Crontab commands should be run in isolation.
Each crontab command will be run in its own context, changing directory in one instruction probably won't lead to that directory being sound for the next executed (they may be run in their own environments, e.g.).
To overcome this, write a simple shell script which encompasses all of your commands for a single action.
# MyCommand.sh
cd /home/sam
./code/imageUpdate > image1.txt
# crontab command
50 17 * * * /home/sam/MyCommand.sh

Resources