Cron-Noob

Hi guys!

Consider the following PERL script,

#!/usr/bin/perl

my $userID = `whoami`;
open(TMP, ">user.txt");
print TMP "$userID";
close(TMP);

Run on the command line it works no problem.
Run using crontab it doesn't work.
Where do I go to check errors from crontab?
I've read things about the cron ENVIRONMENT which I don't understand.
Could someone please explain to me what I need to know about crontab before I start using it?

crontab line looks like this:

8 15 * * * /location/of/my/script/cronprob.pl

Thanking you :slight_smile:

You would have to write a script that loads the environment first then calls your perl script.
The best to avoid interactive shell issues to put in a separate file you call e.g. profile_perl where you have all needed environment variables set, then you execute it in your batch script for cron as:

. ~path_to script/profile_perl
/usr/bin/perl ~path_to_script/cronprob.pl

Ok i'm getting that I need to set some environment variables but what variables are these?
What do I need to set them to and how do I set them (yes this thread is appropriately titled!)

Thanks for your response vbe

You set any env variable you need except DISPLAY, TERM TTY and other keyboard stuff since there arent any for cron...
I would imagine PATH is very important and for the rest, look in your .profile or its equivalent what is set that could be used by your script
to set (since cron uses ~/bin/sh):

PATH=/usr/sbin/:/usr/bin:/usr/ccs/bin:/usr/local/sbin:/usr/local/bin:(etc...)...
export PATH

Supposing all I care about is the /usr/bin directory (assuming that this is the one PERL cares about), can I manually go to this PATH variable and set it = to /usr/bin or are these variables not sitting in a file somewhere like a config file?

/usr/bin is maybe the only thing set in cron with /bin, so if it isnt working then you have more to think about (using libraries? perl can have env settings like java does...)

Is there a way to actually look at these variables and see what they are set to?

to see your environment set just type env

Yes, write a simple one-off cron containing something like

env > /tmp/cronenv.txt

It can be most revealing to find out how basic the environment is when running from cron. It is extremely unlikely that the default $PATH will know about Perl (... unless you have changed it). The normal fix is to write a Shell script which sets up (and exports) the Environment Variables you need and then runs the Perl script.

If you cron is failing for a fundamental reason the error messages will be in unix mail for the owner of the cron.

To answer in any further detail, please post what Operating System and version you are running and what Shell you use for system scripts. You need to post this basic information in every thread.

1 Like

Ok so I found system.env and I see in there:

PATH=/bin:/sbin:/usr/sbin:/usr/ccs/bin

Should I update this to read:

PATH=/bin:/sbin:/usr/sbin:/usr/ccs/bin:/usr/bin

EDIT: Oh never mind. I typed env instead and it looks like /usr/bin is already defined.

But there may be more you need, so do what methyl suggested and compare with your env...

1 Like

Sorry guys I am slowly starting to understand what I need to do. I basically need to make cron do the env command and stick the result in a file so I can see what environment variables IT deals with when it runs.

Ok that done and the result is:

HOME=/home/seitjn
LOGNAME=seitjn
PATH=/usr/bin:
SHELL=/usr/bin/sh
TZ=Eire

---------- Post updated at 04:03 PM ---------- Previous update was at 09:42 AM ----------

UPDATE:

The problem is actually that the following script WILL run using crontab

#!/usr/bin/perl

my $username = "jaymoney";
print "$username\n";

The following script will NOT run using crontab (or rather it will run but it won't get the user name...)

#!/usr/bin/perl

my $username = `whoami`;
print "$username\n";

The difference here being the use of the Unix command to get the user's ID rather than hard coding it.
Anyone have any insight into this?

I'm on SunOS 5.10 if that means anything to anyone :stuck_out_tongue:

Thanks!

Now you are starting to understand...
the command whoami and similar are using things that are initialized at logging time but cron ignores all about that since it is not associated to a terminal to start with...
Did you compare the output of env using cron, and typing in your interactive shell?

Yeah the difference is a lot :slight_smile:

From the cron

HOME=/home/seitjn
LOGNAME=seitjn
PATH=/usr/bin:
SHELL=/usr/bin/sh
TZ=Eire

From the terminal

PATH=/usr/sbin:/usr/bin:/usr/ccs/bin:/usr/dt/bin:/opt/SUNWconn/bin:/opt/SUNWste/bin

It goes on even more than that..

So how do I change the evn stuff for cron?

assuming your shell is ksh-like:

8 15 * * * . ~/.profile && /location/of/my/script/cronprob.pl

On some systems whoami is not in /usr/bin (e.g. /usr/ucb/bin/whoami ). When logged in interactively, what do you get for:

whence whoami
which whoami

I don't recall a problem with running whoami in background, but the command is being phased out. Current recommendation is to use id .

id -nu    # Effective id
id -nur   # Real id

Whence whoami:

whence: Command not found.

Which whoami:

/usr/ucb/whoami

With the id command I can only get output from

id

or

id -a

I looked into my home directory and found a local.profile and tried to run this before my script like vbe and vgersh suggested but I guess my syntax could be wrong

0 17 * * * . ~/local.profile && /path/to/my/script/cron.pl

When I try to execute local.profile it says:

export: Command not found.

where is the export coming from?
stick to your .profile, as vgersh99 showed you, for we have no idea what your local.profile stands for...

When I try to execute the profile file manually on the command line I get the export error

I tried making the crontab look like what vgersh suggested but it didn't work. It still runs the script but doesn't get a value from whoami command.

Please show the output of echo $0 from the command line.