user638009
user638009

Reputation: 223

Crontab and testing a command to be executed

I'm quite new to cron and crontab.

I've edited the crontab file and I need to execute manually one of commands so I can try it and test it beforehand. How do I do that? If it fails, is there a mode that shows the errors?

Upvotes: 0

Views: 1418

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754490

  1. Write a shell script that you can test.
  2. Execute that shell script from the crontab.
  3. Remember that cron provides barely any environment - so your script may have to fix that. In particular, your profile will not be used.
  4. Do not get fancy with what you put in the crontab.
  5. Build a debug mode into your shell script.

No, there isn't specifically a mode that shows errors. Usually, if the cron job witters, the output is emailed to you. That is, it sends standard output and standard error information to you if the executed command writes anything to either standard output or standard error.

On MacOS X (10.6.7), the environment I got was (via a crontab entry like 12 37 17 5 * env >/tmp/cron.env):

SHELL=/bin/sh
USER=jleffler
PATH=/usr/bin:/bin
PWD=/Users/jleffler
SHLVL=1
HOME=/Users/jleffler
LOGNAME=jleffler
_=/usr/bin/env

Of those, PWD, _ and SHLVL are handled by the shell. So, to test your script reliably in a cron-like environment, use:

(cd $HOME
 env -i \
    SHELL=/bin/sh \
    USER=$USER \
    PATH=/usr/bin:/bin \
    HOME=$HOME \
    LOGNAME=$LOGNAME \
    /path/to/script/you/execute ...
)

The -i option to env means 'ignore all inherited enviroment'; the script will see exactly the five values specified plus anything the shell specifies automatically. With no arguments, env reports on the environment; with arguments, it adjusts the environment and executes a command.

Upvotes: 1

ripat
ripat

Reputation: 3236

To execute a script "manually" you first have to make it executable by doing:

$ chmod +x yourScriptName

Then do either

$ ./yourScriptName

if you execute it from its path or

$ /full/path/to/yourScriptName

from anywhere.

Upvotes: 0

Related Questions