Link to home
Start Free TrialLog in
Avatar of celtician
celticianFlag for American Samoa

asked on

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!!
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of celtician

ASKER

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...
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.
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?
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.
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
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
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?
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Thanks! the issue was there with the relative path, when setting the full path its working without a problem!!
My first comment was to deal with the paths.
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.
I appreciate Arnold efforts, but unfortunately i could understand Tintin better :(
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.