1823251 Members
3328 Online
109648 Solutions
New Discussion юеВ

Crontab in korn shell?

 
Henry Chua
Super Advisor

Crontab in korn shell?

When I submit my cron job to the crontab, it prompted me "warning: commands will be executed using /usr/bin/sh". what if I have used korn shell for my script, is there anything I do to save me from rewritting is another shell?

just to confirm I have add my job as follow:
"5 * * * * /users/tester/BACKUP_LOG/main.sh" for it to run at every 5 mins, is this correct?

9 REPLIES 9
Thierry Poels_1
Honored Contributor

Re: Crontab in korn shell?

Hi,

cron itself runs with the default Posix shell but you can run your programs through any shell:

either specify it with the instruction:
5 * * * * ksh program.ksh

or specify it in the shell script itself:
#! /bin/ksh
# "#!" as first characters in script will define the interpreter.

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Thierry Poels_1
Honored Contributor

Re: Crontab in korn shell?

ooops forgot second question:

5 * * * *
will run the job 5 minutes past every hour: 00:05, 01:05, 02:05, .... 23,05

To run it every 5 minutes you'll need to specify the "minutes":
0,5,10,15,20,25,30,35,40,45,50,55 * * * *
(sorry no easier way)

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Sanjay Kumar Suri
Honored Contributor

Re: Crontab in korn shell?

The first line of any script should indicate the shell to be used (for example)

#!/bin/sh

UNIX will attempt to execute the file in the current shell, and try to process the included command strings within the file using the syntax rules of the current shell.

So, if you are using the Bourne Shell as your default environment and the ASCII file contains a list of UNIX command structures formatted how the Bourne Shell likes them to be formatted, all will work fine.

However, if you try and execute a C Shell file with a different syntax structure, the Operating System will complain about unrecognised commands and syntax errors.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Henry Chua
Super Advisor

Re: Crontab in korn shell?

Hi Thiery,

It seems strange, I have done wat you say, but the script cant seem to run well in crontab, however if u execute it from the command line, it works without any problem.. can you think of any reason for this?

***It goes directly to the second while loop ignoring the first one..

regards
Henry
Thierry Poels_1
Honored Contributor

Re: Crontab in korn shell?

hi,

I guess the difference is the "current directory".
Some files are put in the current directory as no full path is supplied. When the job is started through cron it is started in the user's home directory. Where did your run the job manually?

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Nguyen Anh Tien
Honored Contributor

Re: Crontab in korn shell?

In crontab you write:
5 * * * * /usr/bin/ksh /users/tester/BACKUP_LOG/main.sh
In main.sh cript you specify ksh by
#!/usr/bin/ksh
....contents of main.sh...
...
Best regard.
PS: You should refer (Shell Basic Guide)
http://www.docs.hp.com/en/B2355-90046/index.html
HP is simple
john korterman
Honored Contributor

Re: Crontab in korn shell?

Hi Henry,
one thing that could cause a problem is the first of these three lines:
find $SDT > map2.txt
diff $MAP2 $MAP1 > $DMAP
cp $MAP2 $MAP1

where the find command produces output in the current directory - whatever that might be - and perhaps it should have been put elsewhere, e.g.:
find "$SDT" > $MAP2

Just a suggestion....

regards,
John K.
it would be nice if you always got a second chance
Bill Hassell
Honored Contributor

Re: Crontab in korn shell?

Several points:

- /bin doesn't exist in HP-UX and hasn't been there for more than a decade. Just like Solaris and other SysV flavors of Unix that adopted the V.4 filesystem layout standard, the correct directory is /usr/bin. /bin (and /lib) in HP-UX are actually temporary symbolic links that may not be installed automatically in future versions. The HP-UX POSIX shell (which is *NOT* the Bourne shell) is actually a superset of Korn shell. In facr, Korn, BASH and HP's POSIX shell are all "POSIX" shells and virtually identical for shell scripting. The real Bourne shell is located in /usr/old/bin/sh.

- As mentioned, the desired interpreter is used when the (strongly recomended) standard #! line is used in every script. This allows any interpreter (/usr/bin/sh, /usr/bin/ksh, /opt/perl/bin/perl, /usr/local/bin/bash, etc) to be used at either a shell prompt or in an at or cron job.

- The cron environment is VERY different than your login shell. cron does not login and therefore /etc/profile and .profile are not executed and you'll end up with:

HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

That's from the crontab man page. That's why your script isn't working the same way--it depends on your login shell environment.

So when you write scripts for cron:

- Always include #! as line 1 with the dfesired interpreter. cron will start your script with the POSIX shell, then switch (if necessary) based on line 1.

- Always hardcode your PATH inside the script. Never include directories you don't need and never include . or .. for file references. Always use fullpathnames for files so there is no ambiguity about exactly what you want.



Bill Hassell, sysadmin
Steven E. Protter
Exalted Contributor

Re: Crontab in korn shell?

We commonly include scripts using serveral different shells in cron.

cron itself uses Posix, with no environment. On a day to day basis our cron runs posix jobs, korn shell jobs and a few c shell and bsh jobs that certain applications require.

It all works, last time I checked the cron log.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com