CRON : Scheduling task - linux

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

Related

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.
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

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 script runs manually in terminal but is not executed from crontab

I have a bash script that I want to be executed every 15 minutes, so I added this line to my crontab:
7,22,37,52 * * * * /path/to/my/script.sh
I've checked the directory path to be correct and the script runs correctly if I just run /path/to/my/script.sh manually from any directory. I have this bang line in my script:
#!/usr/bin/env bash
My script also references other scripts in the same directory as it, and I have run chmod +x on all scripts that are needed. I set the MAILTO to my email address and I was getting some Cron Daemon emails when I changed the line in my crontab to:
7,22,37,52 * * * * sh /path/to/my/script.sh
But I never received emails upon using
7,22,37,52 * * * * /path/to/my/script.sh
or
7,22,37,52 * * * * bash /path/to/my/script.sh
I made sure cron is running and I've also tried redirecting the output of my script to a log file, which is also only written in when I include the sh. However, if I run sh /path/to/my/script.sh from the home directory, it does not work. The only ways my script actually runs is if (from any directory) I call /path/to/my/script.sh or bash /path/to/my/script.sh. I'm pretty new to writing bash scripts so any help is very welcome.
#pvas The cron user environment should be treated with extra special care. The assumption that most users have is that they will have access to paths, directories, permissions etc. This is far from the case. Cron runs in a minimal environment and you must set up EVERYTHING - Paths, Permissions and the location where the scripts are running from.
1) I set up the environment myself.
2) I use fully expanded paths in my crontabs.
3) I make sure any directories that need to be read have read permissions.
4) I make sure that my password does not expire because that will block cron when it does.
5) Make sure underlying scripts are explicitly invoked (by Perl, Bash, Python whatever).
6) Pipe the command on the cron line to a LOG file (even better a log file with a TIMESTAMP).
Fix these things and then try again. Cron is particular, you need to set up everything.
For example:
#SETUP ENVIRONMENT
SHELL=/bin/bash
source /home/userfoo/.bash_profile
#RUN THE SCRIPT everyday at 11:50pm (23:50)
50 23 * * * userfoo /home/userfoo/script.sh >> LOGFILE.txt
<<
Crontab entries should have the following format
m h dom mon dow command
which confirms that your entry below
7,22,37,52 * * * * /path/to/my/script.sh
is correct. Having said that, you must close the crontab editor(:wq) for the changes to come to effect.
It is suggested you go through [ this ] cross site post which portrays the possible issues with cron jobs.
More about hashbang [ here ].

cronjob not executed

I have a problem running a cronjob. No experience with it, so probably overseeing something nooby. The following script works like a charm (all old filters are deleted from db) when run from the shell:
dude#linux:~> /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
I made the script executable with chmod. Now I want this to run regularly using a cronjob:
dude#linux:~> crontab -e
This file was empty, and I placed this on a single line:
* * * * * /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
I expect the script to run every minute, but nothing happens. In /etc/cron.deny, only 'guest' is mentioned, and /etc/allow does not exist. Restarting my system did not help as well.
The crontab seems to be updated proberly:
dude#linux:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.f9Et2M installed on Thu May 3 14:04:47 2012)
# (Cronie version 4.2)
* * * * * /usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb
But I expected here a bit as well:
dude#linux:~> atq
dude#linux:~>
The cronjob does something. Every minute an entry is added to /var/log/cron.log:
2012-05-03T15:27:01+02:00 linux /USR/SBIN/CRON[5276]: (dude) CMD (/usr/bin/env /home/dude/RubyOnRails/myproject/script/rails runner /home/dude/RubyOnRails/myproject/script/delete_old_filters.rb)
The problem is, that the job is not executed. It should remove some records from the database, but it doesn't. Running the same script manually does the trick.
Anyone seeing the (perhaps trivial) thing that I missed?
Perhaps this will help, using the brackets.
* * * * * ( colon separated commands-to-execute )
Check it.

crontab doesnt work in linux

My Linux version is red hat enterprise linux server release 5.3 tikanga
i have schedule crontab as below
1 * * * * /usr/testjob.sh 2>&1 >> /usr/result.txt
crontab job not running on scheduled time...
Please suggest..
Try this at first.
* * * * * /usr/testjob.sh
Then you may received a mail for every minutes. Check the error output.
Sometimes, it may caused by your default shell is just sh instead of bash.
So, maybe ">>" is not supported.
You should check do you have /usr permission when you want to write into it.
As said by +Shawn Chin, if you want to run your command only once, the at command is your friend.
If you want to run your command repeatedly, then you are right to use the cron framework. The manual page explaining the fields of the crontab may be obtained with the following command:
$ man -s 5 crontab
You appear to be in an Indian time-zone (IST). You may have to specify that into the crontab. For instance, using the 'crontab -e' command (to save and quit, type 'ESC-wq', as the editor is VI by default):
#
CRON_TZ=IST
# run at 06:33 (am), every day
33 06 * * * /usr/testjob.sh >> /usr/result.txt 2>&1
Note that '2>&1' should be placed AFTER '>> /usr/result.txt', not before.
just to mention it and make sure
NOTE: Each cron table entry must have a trailing line break in order
for the cron table entry to be recognized.

Resources