Unable to run the cronjob in ubuntu 10.10 server, EC2 instance - linux

I have a AWS EC2 instance with ubuntu 10.10 server.
I am trying to add a cron job to the list. But the cron job is not being executed.
I am actually uploading a particular file to aws s3 using s3cmd visit s3tools.org
What will be in the problem and also the solution.
Kindly help me out
Here is the bash script which has to be run
s3cmd put file-name s3://bucket_name/foder_name/file-name
Here is the job
bash /path/to/file.sh

Are you aware that the global crontab (/etc/crontab) has a user field:
# m h dom mon dow user command
While the crontab of a user (reachable by running crontab -e as a user) does not?
# m h dom mon dow command
This drove me crazy once: cron was failing relatively silently...
That said, try and make a very simple cron entry, maybe directly inline, that touches a file in a writeable folder. This way you'll figure out if it's your script or cron that's being difficult.

Is cron running? you can test this by adding to your crontab:
* * * * * /bin/date >/tmp/the_time
is my crontab being called? You can test this by adding to your cronscript:
echo "Hello world!" >/tmp/the_hello
Does my cronscript get the right path-settings when it is invoked?: add to your cronscript:
set >/tmp/the_settings
In most cases the scripts that are called from cron need to have most of their pathnames hardcoded, sometimes even PATH needs to be set or expanded.

was able to figure out the solution for this. The problem was I need to specify absolute path thats /usr/local/bin/s3cmd .

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

Using Omnilight Yii2 Scheduling

I have installed this extension to assist me in my cron jobs omnilight/yii2-scheduling. The extension has good documentation that is why I settled on it amidst all the other cron-jobs extensions available for yii2. However, I am stack at some place that I need assistance. There is a place where I am asked to put a single line of code on the crontab:
* * * * * php /path/to/yii yii schedule/run --scheduleFile=#console/config/schedule.php 1>> /dev/null 2>&1
However, I am not sure where to place it, i.e. where is the crontab in yii2? anyone who has used this extension and is able to get it running to assist me here.
Maybe a little later but you must put that in the crontab, a cron is :
cron is a Unix, solaris, Linux utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon
and the crontab is where are stored all this crons. To edit this file you must use in terminal:
crontab -e
Put that line here. Save and you are ready to go.

Trouble with running a shell script via a Cronjob on an EC2 Instance

I am trying to to run a cronjob of a script on an EC2 Instance but it's simply not working
The way I am running it right now is
15 * * * * root /home/ec2-user s3_upload.sh TestBucket
I wrote this when I did crontab -e
Right now I am in this directory /home/ec2-user/ so I don't know if maybe that has something to do with why it's not running.
I just don't understand this whole cron thing and how permissions work.
Any help is greatly appreciated!
You can't specify a username in a user's crontab, only in /etc/crontab.
You can't specify a directory in any form of crontab. To change directory, use cd.
The environment is not the same. If you e.g. set PATH in .bashrc, these will not be automatically included, so it's safest to use full paths.
As root (assuming you want to run this as root and not ec2-user or something), run crontab -e and add
15 * * * * cd /home/ec2-user && /full/path/to/s3_upload.sh TestBucket
Make sure you can copy-paste the command in a root terminal and get it working, and read root's mail afterwards to see the command's output with possible error messages. If it still doesn't work, make sure you have these errors available.

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.

How to test a cron job?

I'm using Ubuntu Linux 10.0.4. I want to run a script every 6 hours, every day. When I issue sudo crontab -e, I see:
# m h dom mon dow command
* 00,06,12,18 * * * /opt/scripts/selenium/run_nis_inf_tests.sh
However, I'm not seeing the expected outcome from my script, and I'm not even sure if its running. Is there a way to test, short of waiting until the specified time, that the script is running properly. Or, how can I view the errors the script is generating? - Dave
You can update the MAILTO variable to your email address, and cron should email you any STDOUT and STDERR output. Also check your syslog file /var/log/messages to see if the script is being executed by cron.
-Tony
Cron should mail it results so it looks like you have a problem.
Here, it seems you are missing a user to run the script as :
00,06,12,18 * * * user_name /opt/scripts/selenium/run_nis_inf_tests.sh
replace user_name by the name of the user the script needs to be run by, verify permissions of "run_nis_inf_tests.sh" and you should be ok.

Resources