Saturday, July 10, 2010

CRON - How to (Summary) on Debian



Setup
 
To edit schedule setup, type:
 crontab -e

Format: 
#minutes hour day_in_the_month month day_in_the_week command 



By Example:
 
# m h dom mon dow command
MAILTO="name@isp.com.br"

#Executing everyday at 01:05 AM: 
5 1 * * * /usr/bin/pgm 2>&1 >> /var/log/cron.log 
 


# 00:30 AM on 1st day of Jan, June and Dec. 
30 0 1 1,6,12 * command 


# 08:00 PM on every weekday Mon and Fri in Oct. 
0 20 * 10 1,5 command 


# midnight on 1st, 10th and 15th every month. 
0 0 1,10,15 * * command 


# 00:05 and 00:10 AM every Monday on 10th of every month.  
5,10 0 10 * 1 command 


# redirecting stderr and stdout to the same file
3 0 3 * * * command 2>&1 >> /var/log/cron.log
4 0 2 * * * /usr/bin/browserbkp 2>&1 >>
/var/log/cron.log



Variables etc.

cat /etc/crontab

You can define or override environment variables, by defining them in the crontab file, one variable per line, in the format:

VARIABLE=value




Environment Variables
 
The following environment variables affect the execution of crontab:

EDITOR
Determine the editor to be invoked when the -e option is specified. The default editor is vi.

LANG
Provide a default value for the internationalisation variables that are unset or null.
If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of
the variables had been defined.

LC_ALL
If set to a non-empty string value, override the values of all the other internationalisation variables.

LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters
(for example, single- as opposed to multi-byte characters in arguments and input files).

LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written
to standard error.

NLSPATH
Determine the location of message catalogues for the processing of LC_MESSAGES .





First Use
 
Run once the command:

crontab -u user -e

After this first time, edit with:

crontab -e

More commands:  

crontab -h
 

Note:
Tests done, using graphical interface to warn about occurred errors didn't work under cron, MAKING IT FAIL.
By now, just use shell scripts with text messages.
Even dialog was tested, and cron fails to trigger the task. (08/07/10 22:50)



Defining Default Editor

export EDITOR=vim




Email
Receive only one cronjob's output in your mail, make sure this package is installed:

aptitude install mailx

And change the cronjob like this:

*/10 * * * * /bin/execute/this/script.sh 2>&1 | mail -s "Cronjob ouput" yourname@yourdomain.com


Stopping cron
 

crontab -r

The crontab -r will deactivate the cronjob and remove the file contents from the server.

Cron fails

Possible causes:

 
Exception or script mal-function
Editing crontab, redirect the stderr to the cron's log or another file log.
Check the script manual, running on the environment out of cron.

References to paths
Use absolute paths defined inside the script.

Different environment:
- The environment is different because the users login scripts
(.profile, .cshrc, .bashrc, etc) don't get sourced by cron
- Some versions of cron expect the script to be in 'sh', and ignore
the #! line at the top. You might have to write an 'sh' wrapper for
your script.
- Something in the script expects a TTY to be associated, and doesn't
work without one.



CRON FAILS - WHAT TO DO?
 
If using debian, the environment variables are usually already set, but it's a good practice to check them again.

To discover what happend, redirect stderrr to a log file.
For instance:

05 16 * * * /absolute_path/command options 2>&1 >>/absolute_path/cron.log
22 22 * * * /absolute_path/command options 2>>/absolute_path/cron.log


Path's errors
 

The output redirected to a log file returned this:

/usr/bin/bkpenvSizeWatcher: line 5: /bin/java: No such file or directory

Include the absolute paths on the script and overwrite the enviroment variables with local values.
Example:

JAVA_HOME=/home/server/jdk1.6.0_20 #0 this line overwrites environment variables (required by cron executions)
...
"$JAVA_HOME"/bin/java -jar file.jar [arguments]


X11 DISPLAY error
 
If running a java file, you can get this stderrr message:

Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.(Window.java:432)
...
 
Insert into the script the following local variables:

JAVA_HOME=/home/server/jdk1.6.0_20 #0 this line overwrites environment variables (required by cron executions)
export DISPLAY=:0 # required by cron execution (debian)
 


If you still get stderr messages like this:

Exception in thread "main" java.lang.InternalError: Can't connect to X11
window server using '0:0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$100(X11GraphicsEnvironment.java:52)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:155)
 

Try others diplay settings:

export DISPLAY=0:0
or
export DISPLAY=:0.0
 
If Red Had, try: 

install Xvfb rpm (X virtual frame buffer)

Add to /etc/profile file:
# Start the X virtual frame buffer (Xvfb)
if [ -f /usr/X11R6/bin/Xvfb ]; then
/usr/X11R6/bin/Xvfb :1 -screen 0 1024x768x16
fi
# Set the DISPLAY variable:
export DISPLAY=localhost:1.0

No comments:

Post a Comment

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...