Can't execute script on crontab, but works with manual launch

There is some script that's working flawlessly using my own user, however when setting it to be launched on crontab it is not working correctly, no variables are being written (there are some variables in the script that write some value but this value seems to be null).

I gues it has something to do with the enviroment variables, how can i fix this?

I did the next to find out my "env" variables.

When executing env directly on command line from my user i get:

 env.output
_=/usr/bin/env
MANPATH=/usr/man:/usr/share/man:/usr/dt/man:/usr/openwin/man
LANG=C
SSH_TTY=/dev/pts/11
PATH=/usr/bin:/usr/ucb:/etc:.
LC_ALL=C
EDITOR=vi
LOGNAME=userName
MAIL=/var/mail//userName
PS1=machine1:$LOGNAME:$PWD$
USER=userName
DSM_LOG=/var/opt/adsm
SHELL=/usr/bin/ksh
JAVA_HOME=/usr/jdk1.6.0
HISTSIZE=1000
TMOUT=1800
HOME=/home/userName
ANT_HOME=/usr/local/ant/apache-ant-1.5.4
SSH_CONNECTION=10.51.233.7 57892 10.144.1.21 22
SSH_CLIENT=10.51.233.7 57892 22
TERM=xterm
PWD=/home/userName/scripts
TZ=Europe/Madrid
A__z="*TMOUT

Open in new window



And the same command from the crontab being redirected to some text file returns:

HOME=/home/username
LOGNAME=userName
PATH=/usr/bin:
SHELL=/usr/bin/sh
TZ=Europe/Madrid

Open in new window


What should i change to make scripts on crontab work just like if they were launched manually??

thanks!!
LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
What is in the script? The only thing common to issues such as you report deal with inability of the script to locate commands that do not include direct paths.
i.e. define JAVA_HOME, ANT, PATH, atop the script What is the first line of the script #!/bin/bash?


If you use ENV Variables within the script, make sure they are defined within the script and not expect that they will be defined when run through crontab.
0
celticianAuthor Commented:
This is the script (that calls another script which calls to another one in a remote  :) )

#!/bin/bash
#!/usr/bin/ksh

VAR=$([b]script2[/b].ksh | egrep 'YES|NO')

echo "$VAR"

MAIL_FILE="testfile.txt"

rm -f $MAIL_FILE


echo "FROM" > $MAIL_FILE
echo "To:Me <me@domain.com>" >> $MAIL_FILE
echo "Subject:LOADED RESOURCE-> $VAR SE HAN CARGADO" >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE

echo $STRING >> $MAIL_FILE

echo ${STRING} >> $MAIL_FILE



if [ "$VAR" = "NO\r" ]; then 
	echo "<h2> RESOURCE NOT LOADED, PLEASE OPEN A TICKET ${STRING%%-*} </h2>" >> $MAIL_FILE
else
	echo "<h2> RESOURCE LOADED SUCCESSFULLY! ${STRING%%-*} </h2>" >> $MAIL_FILE
fi;

mail me@domain.com < $MAIL_FILE

Open in new window


The script2 contains:

#!/usr/bin/expect -f

set timeout 400 

spawn -noecho ssh -tt user@machine2 data/resource_status.sh

expect "Password:*"

send -- "pass01\r"

send -- "\r"


expect eof

Open in new window


If needed i can attach too the code of "resource_status.sh" but i think this would enough, the error must be somewhere around these code lines...
0
arnoldCommented:
you have to decide which shell you want bash or ksh.
note your default shell is ksh, but the top line of the script is bash.
remove line 1 and see if that fixes the issue.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

celticianAuthor Commented:
Erm, why is this working now being launched manually?

i realy dont care either of them, i just want it work, what should i remove?
0
arnoldCommented:
Your shell is ksh, at times the first line in the code is ignored.  Cron usually has /bin/sh as the base shell which means several of your commands/tests are not valid and generate errors.
One thing make sure the line in cron tab does not have redirects to /dev/null and have instead 2>&1
This way any output from the script and errors will be emailed to the user of the crontab.

Remove the first line leaving ksh as the shell.

I would think using /usr/sbin/sendmail through which to send the email versus using the mail client as you have.

Does it need to be HTML formatted?

It is not clear to be besides the shell, what your issue is, allowing from cron to email you the output seen from running the command, could shed light on what is going on.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Try putting /home/username/.profile before the first command in your (cron) shell script. This assuming that username is your user and .profile is your profile. Change to .bash_profile if bash is your shell. Note that when your script is started by cron, you should capture all output or you'll get mail messages from cron etc. A simple way of doing this is:

0 * * * * /home/username/your_script > /home/username/your_script.cronlog 2>&1
0
celticianAuthor Commented:
I tried to remove the first line and it still keeps not working right and not sending the value of the variable.

The ilne in crontab does not have any redirection to anywhere, where should i add the error redirection command??

Gerwin:

I changed the crontab entry for the script to (to be executed here at 23:35, a few mins ago... )

35 23 * * * /home/userName/scripts/script.ksh > /home/userName/scripts/script.ksh.cronlog 2>&1

Its not working yet, but strangely when opening "script.ksh.cronlog" i can't open it, i looks like if the file didn't exist. Its so strange...  This is the error i get: ksh: 35:  not found
0
arnoldCommented:
first, you should make sure that MAIL_FILE is a full path verus just a filename.
What is in STRING? what sets the data into it?


The format of the file in the following format
To:
From:
Subject:

message content

Passing this info to /usr/sbin/sendmail -oi -fyouremailaddress -t
Will transmit the message.


Your shell settings are different, what is it you are trying to get from the VAR assignment? on the 4th line?


You need to eliminate from the script parameters that are only available in the shell.

My default shell is bash. took the first two lines of your #!/usr/bin/ksh and VAR=$(script2.ksh | egrep 'YES|NO')
I get an error on the VAR determination
run the command as ksh  script2.ksh

Everything the script needs to do and use, must be defined and available within the scripts. Any env variables.

Is your query is to check whether the script2 has different formatting?
0
celticianAuthor Commented:
Sorry, i recycled some code from another script and the variable String has no value, its empty, i have already deleted it, makes no difference.

MAIL_FILE  is a variable where i "build" the email text that will be sent later.

script2 will return only YES or NO, and i need to send an email using that info.

I get the point that i should eliminate the parameters that are only available in the shell, but how do i know what are these to be removed?

This is the new code:


#!/usr/bin/ksh

VAR=$(script2.ksh | egrep 'YES|NO')

echo "$VAR"

MAIL_FILE="testfile.txt"

rm -f $MAIL_FILE


echo "From:Machine" > $MAIL_FILE
echo "To:Me<me@domain.com>" >> $MAIL_FILE
echo "Subject:RESOURCE LOADED -> $VAR " >> $MAIL_FILE
echo "Content-Type: text/html" >> $MAIL_FILE
echo "<html>" >> $MAIL_FILE
echo "<body style='font-family:calibri;font-size:14px;'>" >> $MAIL_FILE


if [ "$VAR" = "NO" ]; then 
	echo "<h2> Resource not loaded, please open ticket </h2>" >> $MAIL_FILE
else
	echo "<h2> Resource loaded OK! </h2>" >> $MAIL_FILE
fi;

mail me@domain.com < $MAIL_FILE

Open in new window

0
TintinCommented:
Better to use full paths

Change

VAR=$(script2.ksh | egrep 'YES|NO')

to

VAR=$(/full/path/to/script2.ksh | egrep 'YES|NO')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
What is it you want the script to do?
#!/usr/bin/ksh

VAR=$(script2.ksh | egrep 'YES|NO')

echo "$VAR"




(echo "From: Machine" 
echo "To: Me <me@domain.com>"
echo "Subject: RESOURCE LOADED -> $VAR " 
echo "Content-Type: text/html"
echo
echo "<html>" 
echo "<body style='font-family:calibri;font-size:14px;'>" 

if [ "$VAR" = "NO" ]; then 
	echo "<h2> Resource not loaded, please open ticket </h2>"
else
	echo "<h2> Resource loaded OK! </h2>" 
fi;
)|/usr/shin/sendmail -or -fme@domain.com -t

Open in new window


What is the nature of the VAR test?
0
celticianAuthor Commented:
Thanks! the issue was there with the relative path, when setting the full path its working without a problem!!
0
arnoldCommented:
My first comment was to deal with the paths.
0
celticianAuthor Commented:
I know he referred to paths, but not the path to the script itself, i understood he was referring to enviroment variables paths and such.

Tintin answer was easier to understand for me. so ill accept both answers.
0
celticianAuthor Commented:
I appreciate Arnold efforts, but unfortunately i could understand Tintin better :(
0
arnoldCommented:
You only posted the script later on, all you posted with the question were the env output from cron and your account.
All I could do was to provide a general items to check.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.