Operating System - HP-UX
1830226 Members
1844 Online
109999 Solutions
New Discussion

launch an user script from root crontab

 
Carles Viaplana
Valued Contributor

launch an user script from root crontab

Hello,

I need to launch an script from oracle user in order to check database so I need to schedule execution on crontab.

Problem is I want scheduled scripts are launched from root crontab, so how can I launch an script from oracle user but scheduled from root crontab?

Thanks in advance for your help.
Regards,

Carles
9 REPLIES 9
Joseph Loo
Honored Contributor

Re: launch an user script from root crontab

hi,

if i understand u correctly, u may use the su command in the cron:

# su - oracle -c
regards.
what you do not see does not mean you should not believe
Arunvijai_4
Honored Contributor

Re: launch an user script from root crontab

Hi Carles,

You can use "su" to switch to Oracle and execute script.

#!/sbin/sh
/usr/bin/su - oracle

---Script---

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Steven E. Protter
Exalted Contributor

Re: launch an user script from root crontab

Shalom Carles,

If the script requires user environment, the /usr/bin/su - oracle -c "command"

methodology is required.

If the user environment is not required, root can run any script it wants.

The reason I point out /usr/bin/su is that by default, cron has no envirnoment, no path, to TERM, no nothing. Therefore one must be very specific when giving commands.

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
Devender Khatana
Honored Contributor

Re: launch an user script from root crontab

Hi,

As mentioned earlier create a script with permissions to only root.

Inside that script define the function

#su - oracle -l /path_to_script/script_name

This shall be able to do the needful.

Also redirect the error and standard ouput to some file like this

#su - oracle -l /path_to_script/script_name 2>&1 >>filename

HTH,
Devender
Impossible itself mentions "I m possible"
Eberhard Maehr
Occasional Advisor

Re: launch an user script from root crontab

Hi Charles,
If you run the script with su - oracle -c "script-name" from root crontab, be sure, that for files you may use within in the script you should use absolute pathnames or set the PATH variable in the script. As Steven mentioned in the mail before there are no environment variables.

Regards
Eberhard
Frank de Vries
Respected Contributor

Re: launch an user script from root crontab

we do everything via cron,
monitoring, backups etc..

30 6,12,18 * * 1-6 su - orasys -c "/picnew/backup/bin/backup_ora_autosys.sh" > /tmp/applautosys.log 2>&1

Make sure you declare variables
explicitly: see example script

# does an export of an apps in Oracle : Autosys
#
set -x
print "Running $0 on $(date)"
NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
export NLS_LANG
DATE=$(date "+%d%m%y")
export DATE
ORACLE_SID=autoP1
export ORACLE_SID
ORACLE_HOME=/usr/oracle/product/8.1.7.64
export ORACLE_HOME
APPS=autosys
export APPS
USER=autosys
export USER
EXPORTDIR=/usr/oracle/admin/autoP1/exports/
cd $EXPORTDIR
cp -p $EXPORTDIR/autosys$DATE.gz $EXPORTDIR/autosys$DATE.gz-1
if ( exp system/iatsuaiaop file=$EXPORTDIR/$APPS$DATE owner=$USER compress=n consistent=y direct=true log=
$EXPORTDIR/$APPS$DATE.log )
then
echo "$ORACLE_SID $APPS Export Succesfull"
else
echo "$ORACLE_SID $APPS Export Unsuccesfull" | mailx -s "$ORACLE_SID $APPS Production export NOK" me.
@me.com
exit 1
fi

/usr/contrib/bin/gzip -f $EXPORTDIR/$APPS$DATE.dmp
pwd
ll
exit 0

This is a working example.
Good luck
Look before you leap
Frank de Vries
Respected Contributor

Re: launch an user script from root crontab

we do everything via cron,
monitoring, backups etc..

30 6,12,18 * * 1-6 su - orasys -c "/picnew/backup/bin/backup_ora_autosys.sh" > /tmp/applautosys.log 2>&1

Make sure you declare variables
explicitly: see example script

# does an export of an apps in Oracle : Autosys
#
set -x
print "Running $0 on $(date)"
NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
export NLS_LANG
DATE=$(date "+%d%m%y")
export DATE
ORACLE_SID=autoP1
export ORACLE_SID
ORACLE_HOME=/usr/oracle/product/8.1.7.64
export ORACLE_HOME
APPS=autosys
export APPS
USER=autosys
export USER
EXPORTDIR=/usr/oracle/admin/autoP1/exports/
cd $EXPORTDIR
cp -p $EXPORTDIR/autosys$DATE.gz $EXPORTDIR/autosys$DATE.gz-1
if ( exp system/passwd file=$EXPORTDIR/$APPS$DATE owner=$USER compress=n consistent=y direct=true log=
$EXPORTDIR/$APPS$DATE.log )
then
echo "$ORACLE_SID $APPS Export Succesfull"
else
echo "$ORACLE_SID $APPS Export Unsuccesfull" | mailx -s "$ORACLE_SID $APPS Production export NOK" me.
@me.com
exit 1
fi

/usr/contrib/bin/gzip -f $EXPORTDIR/$APPS$DATE.dmp
pwd
ll
exit 0

This is a working example.
Good luck
Look before you leap
Carles Viaplana
Valued Contributor

Re: launch an user script from root crontab

Hi all,

Thanks a lot for your replies.

I just added "su - oracle -c" to command I got in root crontab and it works fine.

Thanks again.
Regards,

Carles
Vibhu Tewary
New Member

Re: launch an user script from root crontab

Hi,
Thank you so much for the script. I have been trying to start Alfresco using cron.
Since i was not logging the errors, i didn't realize the problem until "Permission Denied" on the log method shown here.

Also setting the variables for the program in the scripts made it run as per schedule.

My hearty thanks.

So my script looks like this now:

for cron:

SHELL=/bin/bash
37 * * * * su - root -c "/opt/alf_script.sh" > /opt/oracle_script.log 2>&1

and the alf_script.sh is:

set -x
export CATALINA_HOME=/opt/alfresco/tomcat
export CATALINA_BASE=/opt/alfresco/tomcat
export JAVA_HOME=/usr/java/jdk1.6.0_04
export JRE_HOME=/usr/java/jdk1.6.0_04/jre
hostname localhost
PATH=$PATH:/opt/alfresco/bin
/opt/alfresco/tomcat/bin/startup.sh

and the log now says:

++ export CATALINA_HOME=/opt/alfresco/tomcat
++ CATALINA_HOME=/opt/alfresco/tomcat
++ export CATALINA_BASE=/opt/alfresco/tomcat
++ CATALINA_BASE=/opt/alfresco/tomcat
++ export JAVA_HOME=/usr/java/jdk1.6.0_04
++ JAVA_HOME=/usr/java/jdk1.6.0_04
++ export JRE_HOME=/usr/java/jdk1.6.0_04/jre
++ JRE_HOME=/usr/java/jdk1.6.0_04/jre
++ hostname localhost
++ PATH=/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/alfresco/bin:/root/bin:/opt/alfresco/bin
++ /opt/alfresco/tomcat/bin/startup.sh

after running a chmod 777 on the alf_script.sh

Thanks and Regards,
Vibhu.