sudo command is not working inside a script when placed in cron

Hi All,
i have a cron entry like

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /amex/sssmonitor/dss_chk.ksh

and the script is like

#!/bin/ksh
file=`uname -n`
> /sunmast/projects/oasis/COREDEV/Dss$file.log
> /tmp/output_sss
today=`date`
varb=`ps -ef | grep java | grep sss | grep -v grep | wc -l`
if [ "$varb" -ne 1 ]
then
echo "FAILED : SSS processes down, Recycling SSS " >> /sunmast/projects/oasis/COREDEV/Dss$file.log
sudo /etc/init.d/init.sss start >> /tmp/output_sss
else
echo "SSS process is running successfully on `uname -n` on $today " >> /sunmast/projects/oasis/COREDEV/Dss$file.log
fi
MEMSIZE=`ps -e -o vsz,args | grep /amex/dss/sss/java | grep -v grep | cut -d' ' -f1`
echo " Memory usage by dss java client in kilobytes is $MEMSIZE on $today " >> /sunmast/projects/oasis/COREDEV/Dss$file.log

when i am manully running this script sudo command inside the script is working,But when the cron is executing the sudo command is not working inside the script.
Could anyone let me know what could be the possible reason.
I have checked that the cron is executing as other command are running except the sudo command.

Please help.

Thanks:(

Your sudo line should be something like:

sudo -u <user> -c "/etc/init.d/init.sss start >> /tmp/output_sss " 

But you would have to look the mans, the syntax varies depending of the version...

It's probably got something to do with the way you have sudo setup to handle passwords. There are a variety of options:

  1. sudo will always ask for a password.
  2. sudo will only ask the first time it's run
  3. sudo will require no password

I'm willing to bet that your script works in your shell because sudo is set to only prompt for a password the first time. When cron calls the script, it is probably hanging waiting on a password to be entered.

I'd look at your /etc/sudoers file to see how you're setup.

Or, you could just put this job into the root crontab and not worry about sudo at all.

I cannot access that file /etc/sudoers as i am not having the access.
Is there any other way other that running the script in root cron??

i tried this also sudo -u <user> -c "/etc/init.d/init.sss start >> /tmp/output_sss "
but it is also not working...

Well, if you have access to sudo as root, you can modify the sudoers file:

sudo visudo

Or, like I said, you can just modify the root crontab:

sudo crontab -e

If you don't have access to the root account and sudo is not working for you, then you will not be able to run root commands until someone with proper privileges gives you access.

The -u is an option to give the users name you want to be when executing the command/program...

Type sudo -l to see what you are entitled to

What OS are you on? By the look of it it seems linux / OSX which have different behaviour