1820390 Members
3674 Online
109623 Solutions
New Discussion юеВ

crontab on solaris

 
SOLVED
Go to solution
amonamon
Regular Advisor

crontab on solaris

Hello everyone..
I have problem when executing script...from command line it works perfect but when I add it to cron it starts going crasy..I know that it might be connecteed with my path variable but how can I fix that so that my script works same like from command line ./myscript

Thanks..:)
19 REPLIES 19
Indira Aramandla
Honored Contributor

Re: crontab on solaris

Hi Amonamon,

When you run a script manually it goes back to environment initialisation. When you run the same script through Cron, it does not run profile therefore you have to initialise the variables in the script or specify the full path.


IA
Never give up, Keep Trying
Sivakumar TS
Honored Contributor

Re: crontab on solaris


Hi,

cron invokes the command from the user's HOME directory with the shell, (/usr/bin/sh).

cron supplies a default environment for every shell, defining:
HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Please make sure the ENV is set properly. That could be main suspect.

Regards,

Siva.
Nothing is Impossible !
Arunkumar.B
Trusted Contributor

Re: crontab on solaris

Hi Amonamon,

In crontab u have to specify the fullpath of the command which need to execute..


Cheers
Arunkumar.B
Necessity breaks iron
amonamon
Regular Advisor

Re: crontab on solaris

First let me thank for answers,

I am new in shell scripting..and I have been reading about cron..but I am not sure what should I do I am root user...with my env:
PWD=/var/adm
TZ=MET
_INIT_RUN_NPREV=0
HZ=100
HOSTNAME=xxdmpp
_INIT_RUN_LEVEL=3
LD_LIBRARY_PATH=/opt/SMAW/lib
_INIT_UTS_ISA=sparc
_INIT_UTS_SYSNAME=SunOS
_INIT_UTS_RELEASE=5.8
MANPATH=/usr/share/man:/opt/SMAW/man
_INIT_PREV_LEVEL=S
MACHTYPE=sparc-sun-solaris
MAIL=/var/mail/root
_INIT_UTS_VERSION=Generic_117350-05
JAVA_HOME=/opt/SMAW/SMAWjdk/1.2/
TELEESC=~
TELEROOT=/opt/SMAW/SMAWtssv/tele
LOGNAME=root
_INIT_UTS_NODENAME=xxdmpp
_INIT_UTS_PLATFORM=FJSV,GPUZC-M
SHLVL=1
_INIT_UTS_MACHINE=sun4us
SHELL=/bin/bash
HOSTTYPE=sparc
OSTYPE=solaris
HOME=/
TERM=vt100
PATH=/usr/sbin:/usr/bin:/opt/SMAW/bin:/opt/SMAW/sbin:/opt/SMAW/SMAWtssv/tele/bin
TELETAG=TELE
_INIT_NET_STRATEGY=none
_=/usr/bin/env
OLDPWD=/var/adm/log


and I user root crontab but my machine goes crasy basicly script does not work on good way..on a way it works from command line..
script is written in #!/bin/bash


Please give me a hint..

Cheers,
Sivakumar TS
Honored Contributor

Re: crontab on solaris

Hi,

Also use script with the obsolute PATH,


30 18 * * * /


Regards,

Siva,
Nothing is Impossible !
Arunkumar.B
Trusted Contributor

Re: crontab on solaris

HI Amonamon,


Can u pls attach the crontab entry which u made ..


Cheers
Arunkumar.B
Necessity breaks iron
amonamon
Regular Advisor

Re: crontab on solaris

My crontab entry is:

* 9,10,11 * * * /opt/tomcat/logs/do/myscript


and I put othing else in cron..this is cron from root..

Regards,
amonamon
Regular Advisor

Re: crontab on solaris

anyone has a hint...how could I solve this??

thanks in advance..
Chan 007
Honored Contributor

Re: crontab on solaris

Hi,

I am not sure what the script does.

But I have few suggestion

1. * 9,10,11 * * * /opt/tomcat/logs/do/myscript

Change the * (Mins to a meaning full time like 30, 35 ot even 42)

Check your cronlog, also paste it.

Chan
amonamon
Regular Advisor

Re: crontab on solaris

my script is very simple just simple copying...and where can I find cronlog to be able to sent U??

thanks...
Bill Hassell
Honored Contributor
Solution

Re: crontab on solaris

For any script, you have to know what the commands in the script require. All Unix boxes run cron jobs on behalf of the user -- there is no login. So all the settings in /etc/profile and .profile, .bashrc, etc are missing. The only way to write a shell script for cron is to assign all the variables inside the script first. Your env command shows the environment so start by copying all the variables into your script. I prefer to add only the variables that are required as the script will be simpler.

Another technique is to source the login profiles in the script. For bash or ksh, use the . (dot or period) as the command to run the profiles as in: . /etc/profile and . $HOME/.bashrc, etc Note that if the profiles do not properly protect interactive commands (commands for terminal usage like tabs) then you'll get some warning messages about "not a typewriter" because cron has no terminal.


Bill Hassell, sysadmin
Chan 007
Honored Contributor

Re: crontab on solaris

Hi log should be /var/adm/cron/log.

Check what Bill said.

Try so that you can get the log file. Change your script accordingly. Check the /tmp/t.log

00 15 * * 1-5 /tmp/t.sh >/tmp/t.log 2>&1

Chan
amonamon
Regular Advisor

Re: crontab on solaris

Thaks for answers...
But I am new in solaris...and this is machine in production...and before I do anything let me ask U one more thing..
when I type env I get all those variables..can I just copy them as they are and inport it simply in headear of my script??
justi like this:

myscript:

#!/bin/sh
d=`date +%d%m%y%H%M%S`
dir="/opt/tomcat/logs"
/bin/rm $dir/me/old
/bin/mv $dir/me/new $dir/me/old
/bin/cp $dir/catalina.out $dir/me/new

/bin/diff $dir/me/new $dir/me/old > $dir/me/diffre

/bin/chmod 777 $dir/me/diffre
/bin/grep "no stack trace available" $dir/me/diffre > $dir/me/newfile
...
....
......


and bake my script like this:


#!/bin/sh
_=/usr/bin/env
MANPATH=/usr/share/man:/usr/local/man
HZ=100
LC_MONETARY=en_US
LC_TIME=en_US
PATH=/usr/sbin:/usr/bin:/usr/local/bin:.:/usr/opt/SUNWmd/sbin
LOGNAME=me
MAIL=/var/mail/me
PS1=anes@machine:$PWD
>
LC_MESSAGES=C
LC_CTYPE=en_US
SHELL=/usr/bin/ksh
HOME=/home1/scriptPOOL
LC_COLLATE=en_US
LC_NUMERIC=en_US
TERM=vt100
PWD=/home1/scriptPOOL
TZ=MET

d=`date +%d%m%y%H%M%S`
dir="/opt/tomcat/logs"
/bin/rm $dir/me/old
/bin/mv $dir/me/new $dir/me/old
/bin/cp $dir/catalina.out $dir/me/new

/bin/diff $dir/me/new $dir/me/old > $dir/me/diffre

/bin/chmod 777 $dir/me/diffre
/bin/grep "error" $dir/me/diffre > $dir/me/newfile
...
....
......

if I understand Bill ok??

Thanks in advance..
amonamon
Regular Advisor

Re: crontab on solaris

PS please Bill confirm this..
Kyris
Frequent Advisor

Re: crontab on solaris

I had a similar problem on an HP9000 system and the way I could get round it was to use the 'at' command and use all the env variables it created and copy those at the top of my shell script. This did the trick for me.
Hope this note will help...
amonamon
Regular Advisor

Re: crontab on solaris

as I mention before I am beginnner...problem was in adding my script in cron I put:

* 14,15 * * * /path/to/my/script

so that means script executes every minute I thought that means only on 2PM and on 3PM..I was wrong..it was executing every minute..:)

thanks everyone thru this I learned a lot more things..:)
Chan 007
Honored Contributor

Re: crontab on solaris

Hi,

My question have you tried running the script once, not every minute, see below. change your script to log you error and post the errors.

Cat /tmp/t.log will give you the errors. Post that log.

00 15 * * 1-5 /tmp/myscript >/tmp/t.log 2>&1

Chan
Bill Hassell
Honored Contributor

Re: crontab on solaris

If this is your entire script:

> #!/bin/sh
> d=`date +%d%m%y%H%M%S`
> dir="/opt/tomcat/logs"
> /bin/rm $dir/me/old
> /bin/mv $dir/me/new $dir/me/old
> /bin/cp $dir/catalina.out $dir/me/new
>
> /bin/diff $dir/me/new $dir/me/old > $dir/me/diffre
>
> /bin/chmod 777 $dir/me/diffre
> /bin/grep "no stack trace available" $dir/me/diffre > $dir/me/newfile

then you don't need any additional variables defined. I misunderstood the problem as one where the script was failing with error messages or not performing the required task. It sounds like the script is just running too many times which is simply specifying the cron values correctly.

Here is a simplified version of your script:

#!/bin/sh
PATH=/bin
set -u

d=`date +%d%m%y%H%M%S`
dir="/opt/tomcat/logs"
rm $dir/me/old
mv $dir/me/new $dir/me/old
cp $dir/catalina.out $dir/me/new
diff $dir/me/new $dir/me/old > $dir/me/diffre
chmod 777 $dir/me/diffre
grep "no stack trace available" $dir/me/diffre > $dir/me/newfile

By defining PATH local to the script, you don't need fullpaths for the commands. If you are concerned about aliases (ie, alias rm="rm -i") these won't occur in cron because cron does not login.

The set -u is a relaibility control -- it prevent spelling errors from creating problems. If you misspell a variable name and try to use it, the script will create an error message and stop before any damage is done.

I would also change the chmod 777 to a secure value of 644. 777 has two major problems:

1. You are making the diffre file executable and this file is not a program or sctipt. Never add the executable bit to data files.

2. The file is world-writable and therefore can be destroyed by anyone on your system, either by accident or maliciously. 777 (or 666) permissions are red flags that say to the world: "destroy me"

Most new people in Unix see an error like "permision denied" and quickly change the permissions to 777. But when this does not fix the problem, the bad permissions are left and bad things happen. Always determine the real reason for permission problems . Otherwise, your system will become unstable over time.

As far as your cron entry, a * means all occurances (ie, every day, every hour, every minute, etc). If what you want is for the script to run at 2pm and 3pm, then pick which minute you want to run, perhaps 2:10pm and 3:10pm and your cron entry becomes:

10 14,15 * * * /path_to_script/myscript


Bill Hassell, sysadmin
amonamon
Regular Advisor

Re: crontab on solaris

:))

Bill Hassell Thank You very much for your help and time..I do appreciate it..


thanks everyone for useful hints..