The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sudo command not working with cron job. WhotheWhat Shell Programming and Scripting 6 12-18-2008 04:10 PM
Grep command is not working when put into cron thiru_cs Shell Programming and Scripting 5 08-06-2008 05:39 AM
formatting textfile inside ksh script using awk not working tekline UNIX for Advanced & Expert Users 6 07-03-2007 01:40 AM
script not working in CRON abhijeetkul Shell Programming and Scripting 5 07-13-2006 05:48 AM
looping a array inside inside ssh is not working, pls help reldb Shell Programming and Scripting 5 07-07-2006 10:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
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
  #2 (permalink)  
Old 02-11-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,546
Your sudo line should be something like:
Code:
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...
  #3 (permalink)  
Old 02-11-2009
stanleypane stanleypane is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
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.
  #4 (permalink)  
Old 02-12-2009
usha rao usha rao is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 70
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...
  #5 (permalink)  
Old 02-12-2009
stanleypane stanleypane is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
Well, if you have access to sudo as root, you can modify the sudoers file:
Code:
sudo visudo
Or, like I said, you can just modify the root crontab:
Code:
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.
  #6 (permalink)  
Old 02-12-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,546
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0