Operating System - HP-UX
1832649 Members
3042 Online
110043 Solutions
New Discussion

Re: Shell and cronjob behave differently

 
SOLVED
Go to solution
Kalin Evtimov
Regular Advisor

Shell and cronjob behave differently

Hi!
I have a small problem with my perl-skript, but not a programming problem.
If I run the script from shell with user root, it works perfectly. If i run it from a cronjob with user root, it doesn't work. I don't know why.

Is there any difference between shell and cron, both using the root-account?
11 REPLIES 11
Indira Aramandla
Honored Contributor

Re: Shell and cronjob behave differently

Hi Klian,

Whne you run the script manually the environment variables are pikced from the profile. But not when the script is scheduled through cron.

Cron does not run profile therefore it's up to you to initialise the variables in the script.

So you have to provide the full paths of the scripts when called from cron.


IA

Never give up, Keep Trying
Sergejs Svitnevs
Honored Contributor

Re: Shell and cronjob behave differently

Did you define the environment in which the cron tasks are run?

If the PATH variable is omitted in the configuration file for cron, no default will be used and file locations will need to be absolute.

Regards,
Sergejs
Muthukumar_5
Honored Contributor

Re: Shell and cronjob behave differently

Can you post cronjob information for that script execution. Export PATH and needed variables in the script itself.

~regards.
Easy to suggest when don't know about the problem!
Kalin Evtimov
Regular Advisor

Re: Shell and cronjob behave differently

I always use absolute paths in cronjobs and files that run under cron, but it still doesn't seem to work.
Kalin Evtimov
Regular Advisor

Re: Shell and cronjob behave differently

There is no cronjob output on run. What information should I post?
Steven E. Protter
Exalted Contributor

Re: Shell and cronjob behave differently

You can route cron output via email.

/commandline 2>1& mail -s "subject" email@your.net

There is also a cron log in /var

If there is nothing then cron isn't running.

ps -ef | grep cron

To start:
/sbin/init.d/cron start

Start configuration file(name at bottom)

#!/sbin/sh
# @(#) $Revision: 72.3 $
# Cron configuration. See cron(1m)
#
# CRON: Set to 1 to start cron daemon
#
CRON=1
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/rc.config.d/cron" [Read only] 7 lines, 129 characters

Please check it out and get back to us.

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
Adisuria Wangsadinata_1
Honored Contributor

Re: Shell and cronjob behave differently

Hi,

Check the url below (docID : W1699733) about 'How can I make a script run euid root?' :

http://www2.itrc.hp.com/service/cki/docDisplay.do?docLocale=en_US&docId=200000006432514

Hope this information can help you.

Cheers,
AW
now working, next not working ... that's unix
Muthukumar_5
Honored Contributor

Re: Shell and cronjob behave differently

Login as root user,

# crontab -l
post which script line you are executing there.

# Post cron log entries for this execution as,

cat /var/adm/cron/log

When you are execution from shell then it will use specific user logged now. Cron is behaving like another user to execute scripts. Use su - root -c "script". You have to use sudo for not prompting for passwd.

~regards



Easy to suggest when don't know about the problem!
Kalin Evtimov
Regular Advisor

Re: Shell and cronjob behave differently

Thank you!
I found it.
The problem was, that I didn't use a full path where I was trying to create a new file.
Arunvijai_4
Honored Contributor

Re: Shell and cronjob behave differently

Can you post /var/adm/cron/log ? It may have some information.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arturo Galbiati
Esteemed Contributor
Solution

Re: Shell and cronjob behave differently

Hi,
my suggestion is always load the pofile in the script you schedule in cron.
the good way to do this is:

[[ "$(tty)" = "not a tty" ]] && ./.profile

In this way when you run your script interactively the profile won't be loaded while it will be loaded when you will scheule it in crontab or run by nohup, at, etc.

HTH,
Art