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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sudo command is not working inside a script when placed in cron
# 1  
Old 02-11-2009
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.

ThanksSmilie
# 2  
Old 02-11-2009
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  
Old 02-11-2009
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  
Old 02-12-2009
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  
Old 02-12-2009
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  
Old 02-12-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

2. Shell Programming and Scripting

UNIX command in cron not working

the following unix command gives me the number of records avaiable in my application /navs/sys/appl/bin/record list This gives number of rows and colums. I want to generate one text file everyday at 6:10 AM using cron So I added following entery into crontable #Generate record file at... (6 Replies)
Discussion started by: Nakul_sh
6 Replies

3. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

4. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

5. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. Shell Programming and Scripting

sudo inside a here document not working

Have a sudo statement inside of a here document. It prompts me for a password, but doesnt wait for me to enter my password. Is there a way I can use the command without sudo or anyway that I can enter the password correctly? Eg : while read remotehost do ssh -t $2@$remotehost <<REMOTE... (13 Replies)
Discussion started by: mnanavati
13 Replies

8. Shell Programming and Scripting

sudo chmod not working through script

Legends, I am trying sudo chmod from one shell to different machine. but getting the error. Please let me know if there is any way to do this. A#pk} for i in Bl1 Brl1 >> do >> echo $i >> ssh $i sudo chmod 755 /etc/services >> done Bl1 Password: bash: sudo: command not found ... (2 Replies)
Discussion started by: sdosanjh
2 Replies

9. UNIX for Dummies Questions & Answers

Problem with executing command inside a cron job

Hi All, I have scheduled a script in cron which writes output to the below file. ....>> /data/Target/wrapper_invoke_ds_job_`date '+%Y%m%d'`.ksh_out 2>&1 But the date command is not getting resolved in the format specified. It just resolves to the following. wrapper_invoke_MQ_ds_job_Tue... (3 Replies)
Discussion started by: pkm_oec
3 Replies

10. Shell Programming and Scripting

Sudo command not working with cron job.

Hello, I have written a script that has a sudo command to change file permissions within it. When I run the script manually, the sudo command inside of it works fine. When the script is run through crontab I get the error "cron: not found". It the same user profile that I am using... (6 Replies)
Discussion started by: WhotheWhat
6 Replies
Login or Register to Ask a Question