Showing posts with label cron. Show all posts
Showing posts with label cron. Show all posts

Monday, October 01, 2007

Bad ; sign errors in crontab file, can't install

Editing crontab with PICO editor might display the error:

"bad ; sign errors in crontab file, can't install"

The ; character is used to combine and run multiple commands in one statement. Your cron job might look like:

# Command One; Command Two
*/3 * * * * cd /usr/home/; php -q whatever.php

The above statement consists of two commands.
The second command would be executed even if the first were to fail.

The solution is to get rid of the ; character. Use the && character instead.

The && character will _not_ execute command two if command one were to fail.

To get rid of the error, edit your cronjob like this:

*/3 * * * * cd /usr/home/ && php -q whatever.php

Monday, October 16, 2006

CRON line 1: Unexpected EOF while looking for matching ``'

CRON line 1: Unexpected EOF while looking for matching ``'

When running a CRON job that uses the command:
NOW=`date +%B_%d_%a_%Y`

the script will abort execution and display the following error:

/bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 2: syntax error: unexpected end of file
To prevent this issue from occurring, dump the contents of the script into a .sh file and run the .sh from CRON.

Sometimes, CRON is set to use sh as opposed to the bash shell that the script needs.

The CRON shell can be modified in the /etc/crontab config file. Requires r00t privileges. Ensure that the CRON daemon is restarted after the modifications.

/sbin/service crond restart

Sunday, October 08, 2006

CRON Fields

CRON Fields


# (Use to post in the top of your crontab)
# ------------- minute (0 - 59)
# | ----------- hour (0 - 23)
# | | --------- day of month (1 - 31)
# | | | ------- month (1 - 12)
# | | | | ----- day of week (0 - 6) (Sunday=0)
# | | | | |
# * * * * * command to be executed

* The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"
* The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"
* The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'..

Source: http://en.wikipedia.org/wiki/Crontab


Disable CRON Output From Flooding Email

Disable CRON Output From Flooding Email

When a script is executed through CRON, the output can quickly flood an inbox. To disable output from specific scripts, append the following at the end of the script:

# Disable output completely

*/2 * * * * /usr/local/bin/php /test/foobar.php > /dev/null 2>&1

This script runs every two minutes.
Standard output (1) is redirected to /dev/null
Standard error (2) is directed to the same as standard output (1)

# Redirect script output to a log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt

# Redirect script output to a log file, append log
*/2 * * * * /usr/local/bin/php /test/foobar.php >> /localpath/log.txt

# Redirect script output and CLI error output to the same log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt 2>&1

Note:
/localpath/log.txt - This is standard output (1)
The 2 in the 2>&1 will direct standard error (2) to standard output. In this case, log.txt

# Redirect script output and CLI error output to a different log file
*/2 * * * * /usr/local/bin/php /test/foobar.php > /localpath/log.txt 2>/localpath/cli-errors.txt

# Redirect script output and CLI error output to a different log file, append log
*/2 * * * * /usr/local/bin/php /test/foobar.php >> /localpath/log.txt 2>>/localpath/cli-errors.txt

To test:
Execute this command
ls . IS >> errors.txt 2>>errors.txt

The second alternative is to modify the MAILTO= option and set it to "". This will however, disable all output from being sent to the email address specified in the MAILTO feature.

Another option would be to redirect the output to a text file and call the text file through the browser for analysis.

Note:
The php -q flag suppresses HTTP header output.

Saturday, October 07, 2006

PHP DOCUMENT_ROOT Include does not work with CRON

When including libraries or external files in PHP, the variable $_SERVER['DOCUMENT_ROOT'] will not call the external files if the script is run through CRON.

This is because, usually a PHP script would be executed through a /usr/local/bin/php -q directive. Since Apache does not play a role here, the DOCUMENT_ROOT variable will not work.

To ensure that the DOCUMENT_ROOT works, call the script through curl, wget or lynx. As a security measure, apps like wget are disabled on most servers. The alternative option is to get rid of the DOCUMENT_ROOT variable all together.

When including files in PHP scripts, it is best to create an includes.php in the base dir of the application. All scripts in the sub-directories can call the include file through a define path. The includes file can in turn define paths to other dependencies.

Note:

The -q flag suppresses HTTP header output. As long as your script itself does not send anything to stdout, -q will prevent cron from sending you an email every time the script runs. For example, print and echo send to stdout. Avoid using these functions if you want to prevent cron from sending you email.

Note:
The ../dirname directory include path does not work with cron too. The path needs to be included in full

Source:
http://www.modwest.com/help/kb5-125.html

Further Reference:
http://www.us2.php.net/features.commandline