Cron job every day at 9:27 - cron

I'm having some problem with crontab.
I have this job:
27 09 * * * drush #ildeposito.prod status
I want to execute the cron job every day at 9:27.
But it doesn't work.
If I try to execute the command directly in prompt it works.
What's wrong on my crontab?
If I execute "date" from shell I obtain:
mar 3 set 2013, 12.55.13, CEST
If I put the output of date in a file, with a cron job, I obtain:
Tue Sep 3 12:55:01 CEST 2013
Another: this is NOT executed every minute on 13!
* 13 * * * date >>/tmp/temp_out_hour
This works:
*/15 * * * * drush #site.prod cache-warmer --timeout=6 --hub-pages-file=/home/sergej/websites/sute/site-cache.txt
http://www.site.org
The system seems to have problems with hours.

First things first, you need to specify what "it doesn't work" actually means. Do you mean you see no output? Get no mail sent to you with the output? Typically, standard output from cron jobs will be packaged up in an email and sent to the user running the job. If you want to see what the job is outputting, you can use something like:
27 9 * * * drush #ildeposito.prod status >/tmp/temp_out 2>&1
which will write standard output an error to that file (you also don't need the leading 0 in 09).
If a command works from a shell prompt and not from cron, it's usually a difference in your environment settings. You can test this by replacing your job with a very simple one:
27 9 * * * date >>/tmp/temp_out
and examining that file after it executes. If the date works but drush doesn't, your problem lies outside of cron itself.
cron starts processes with a minimal set of environment variables and you have to ensure you've configured a suitable set. See, for example, here:
We use /usr/bin/env to run drush so that we can set up some necessary environment variables that drush needs to execute. By default, cron will run each command with an empty PATH, which would not work well with drush.
When running drush in a terminal, the number of columns will be automatically deteremined by drush by way of the tput command, which queries the active terminal to determine what the width of the screen is. When running drush from cron, there will not be any terminal set, and the call to tput will produce an error message. Spurrious error messages are undesirable, as cron is often configured to send email whenever any output is produced, so it is important to make an effort to insure that successful runs of cron complete with no output.
In some cases, drush is smart enough to recognize that there is no terminal -- if the terminal value is empty or "dumb", for example. However, there are some "non-terminal" values that drush does not recognize, such as "unknown." If you manually set COLUMNS, then drush will repect your setting and will not attempt to call tput.
Other than that, there are numerous other problems that can plague cron:
having a command on the final line of the file with no newline at the end sometimes causes troubles.
having % in your command somewhere.
having had the cron daemon start with a different timezone setting.
That last one can sometimes be fixed just by restarting the daemon. Given your comments about the bizarre nature of the non-performance, I'd suggest that as a first step.

Related

How to print the output of running program started by crontab job to a file on daily basis

A script start.sh is registered in crontab to run on daily basis
But I'd like to save the output of the program to a file, like cronlog-yyyyMMdd.log
I tried the following but failed:
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date+"\%F")-cron.log 2>&1
Anyone help me?
Using fully-qualified paths to all programs and files that are mentioned eliminate a whole class of errors from debugging issues with crontab entries. In this particular case, doing so would have only proved there were no problems with PATH issues in your crontab entry.
Your issue is that you need a space char between the date cmd and it's formatting argument string, instead of
$(date+"\%F")
#0 ^v
$(date +"\%F")
I commend you for your good crontab debug etiquette; capturing std-err and std-out to a file gives you 1. proof that your command ran when you expected it to (or if you have an error in your time specification, it will show you when the cmd did run). 2. you avoid having your localhost email box filling up with msgs from the cron daemon. 3 Any std-err msgs that were generated are also captured and are in generally in sync and time order with std-out messages.
IHTH.
16 10 * * 1-5 ~/start.sh >> ~/cronlog/$(date +"\%F")-cron.log
Edit: as explained in comments, this answers the question by correcting a typo in the asker's attempted entry

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.

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.

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