Crontab / shellscript help - linux

I've been trying to get my script to run each day at 06:01 AM.
The script fetches data and outputs it to an .xml file like the following.
#!/bin/sh
tv_grab_se_swedb --days 1 --quiet --output=/www/tv/tv.xml
Priviligies
-rwxrwxrwx 1 root root 68 Mar 4 10:31 fetchdata.sh*
Now when i run the script it works and I get my output in the .xml file and its a charm.
So i wanted to add this to crontab to run this script everyday..
Crontab entry
# m h dom mon dow command
0 6 * * * /www/tv/fetchdata.sh
But somehow the tv.xml is always empty after this script has been ran.
Any solution to this? Have i forgotten something?
-Anders

Check to make sure that tv_grab_se_swedb is in a location that is in the $PATH that cron uses.
Probably nine times out of ten, "unexplainable" errors in cron jobs boil down to path issues.

When you run fetchdata.sh from command line, $PATH variable differs from when thist script runs throught cron.
May be tv_grab_se_swedb can't be found in any of $PATH's and can't be executed
Simpliest solution: rewrite fetchdata.sh with fullpath to tv_grab_se_swedb (something like /usr/local/bin/tv_grab_se_swedb)

Related

Ubuntu cron job

I wrote a bash script I saved it as a file getcalls.sh, I used chmod+x on the file to make it executable. It curls csv output from an api into a file in the same directory it runs in. It runs in a directory called data in one of my web folders. When I run it from command line it runs fine. When I try to put it in cron it doesn't run. My cron entry looks like this.
12 03 * * 3 cd /var/www/html/sitedir/data/getcalls.sh
Is the syntax incorrect?
Yes your syntax is incorrect. You must use it this way:
minute hour day-of-month month day-of-week command
But you provide cd command. Use it as follows:
12 03 * * 3 /var/www/html/sitedir/data/getcalls.sh

How to create a relative linux crontab in bash: NOW (date) + 24 hrs

I need a way to create an entry on the crontab that executes a script only once in 24h.
Steps:
Run a bash script to configure a crontab to run a script in 24h (with command date?). This will run only ONCE 24 hs after .
24h later the script runs.
At the end of the script execution, the crontab must be left clean. This means to remove the entry from the crontab. I don´t want to pollute the crontab with deprecated entries.
If you don't want to pollute the crontab, you can use "at"
e.g.
at now + 24 hours
now you will get "at>" prompt, and you'll be able to enter the code which will be executed in 24 hours
You can also execute a shell script using:
at -f shell_script now + 24 hours
additional info can be found here:
https://en.wikipedia.org/wiki/At_(Unix)
and here: http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/scheduling.html

Cron Jobs not running in Linux Mint 12

This is the content of my crontab -e file
#!/bin/bash
6 14 * * * /home/rishi/cront.sh
Also, the cront.sh file has only this
mkdir foo
I have been trying to make this work since the last 2 days. The cront.sh command works when ran from the terminal. But, does not work from crontab.
EDIT
It turned out that just editing the crontab -e using root
did the job. Nothing more had to be done.
Under cron, there is no guarantee that your environment variables (most importantly PATH) will be set proprerly.
Try adding line like this at the top of your crontab:
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
Also, it would be good idea to use full path for mkdir:
mkdir /path/to/my/dir
Also, it would not hurt to make sure that your cront.sh is executable:
chmod +x /home/rishi/cront.sh
After that, it should work.
EDIT
Generic method to debug crontab issues:
At the top of your script to debug, add a line:
set # this should print all environment variables
Execute your script manually, redirect output to some log file1.
Now, edit crontab to be something like this:
* * * * * /path/to/my/script 2>&1 > /path/to/log/file2
Be sure that log file will be writable for your script.
Also, be sure that your script has executable bit set.
Compare log file1 and log file2, paying close attention to env. variables. If they differ, use whatever method you want to set them to be the same. It could be adding lines to crontab, or using export var=value in your scripts.
After that, there is no reason for this to not work properly.
You don't need to first line
#!/bin/bash <--- remove this line
6 14 * * * /home/rishi/cront.sh
Is your script executable?
If it is not. Try running the following command in a terminal.
chmod +x /home/rishi/cront.sh
An example of crontab format with commented fields is as follows:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /your/path/yourscript.sh
try
6 14 * * * sh /home/rishi/cront.sh
or
add first line in cront.sh
#!/bin/bash
or chmod a+x /home/rishi/cront.sh

Crontab for script

My script is under /u01/software/aditya/script/ directory. Name of script is myscript.sh. I am able to run this script and getting output too. I am trying to set a cronjob for this script at 6.30 daily morning. I am doing this as root user. I have done following steps but not getting output.
crontab -e
30 06 * * * sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log
:wq
but not getting any update in hello.log file :( . please help….
First check your cron log file which is usually in /var/log/syslog. There should be entries similar to
Sep 17 06:30:01 localhost CRON[17725]: (root) CMD (sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log)
If not, your script has never been run. This could be due to a broken crontab file. You should make sure that this file always ends with a newline, better insert more than one at the end so that deleting one accidentally won't break the file.
If this line exists in the log file then your script has been run, but didn't generate any output. This can happen due to a different environment when being run via cron.
Also note that >> only redirects stdout, not stderr. If you want to redirect stderr too, then add 2>&1 at the end of the line.
Normally this is caused by a PATH problem. There is a very good chance that myscript.sh calls a command that is not available in the PATH that cron runs with. Some options to fix this are:
Make sure that every command in myscript.sh is a full path-reference (tedious)
Add source ~/.bashrc to the top of myscript.sh
Add export PATH=$PATH:<colon delimited list of paths necessary for myscript.sh to run correctly>
Pick one of the above, or you could also choose one of the options here: Hourly cron job did not run

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