Script not working if crontab scheduled

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Script not working if crontab scheduled
# 1  
Old 02-05-2014
Script not working if crontab scheduled

Hi all,

I'm working to a script with /bin/bash shebang.
The script works perfectly if I run from command line.

The script runs under a non root user and inside the commands are set with sudo command in a such a way they can be run under root, for example (first rows of the script):
Code:
#!/bin/bash

#Command directory di NNM###

dir_nnm=/opt/OV/bin
dir_nnm_psg=/opt/OV/nonOV/Postgres/bin


### Ricavo il nome del sec group custom ####


secgroup_name="`sudo $dir_nnm_psg/psql -d nnm -U postgres -tc "select name from public.nms_sec_group" | egrep -iv "default|unresolved" | sed -e 's/^[ \t]*//'`"


I'd like to run in crontab under that non root user but when it starts under crontab the variables are set to empty.

Why this behaviour?

Last edited by Franklin52; 02-05-2014 at 07:39 AM.. Reason: Please use code tags
# 2  
Old 02-05-2014
And the scripts runs fine when you execute it ?
# 3  
Old 02-05-2014
yes it does
# 4  
Old 02-05-2014
That means it is set correctly ( or something else missing? ) in your environment, not in the script... why dont you expoprt the variables? If that doesnt solve, then soucre your profile in the script, if that works, copy your profile ot profile4cron so you scource that one
And since you are like unsastified to not know why exaclty, I would comment a profile and uncomment one line after the other to see what was missing...
# 5  
Old 02-05-2014
If the script shown is executed by cron, be sure to use full path to applications inside the script..
Eg: /usr/bin/sudo, /usr/bin/sed, just like you had done for psql using the $dir_nnm_psg..
For egrep i'd alias it like:
Code:
alias egrep="/usr/bin/grep -e"

Hth

EDIT:
Also, if you need to enter the password for sudo, it wont work at all, as there is no "input box" or 'terminal' shown to the user, never (afaik).
Further, sudo executes the command as root, so why not execute the script as root directly in the first place?
# 6  
Old 02-05-2014
Quote:
Originally Posted by sea
Also, if you need to enter the password for sudo, it wont work at all, as there is no "input box" or 'terminal' shown to the user, never (afaik).
This is because programs run by cron have no terminal, and sudo uses the current terminal to get a password.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script when scheduled in Crontab gives extra records in output

Hi, We have created a script that's checks the latency of IIDR subscription by fetching details from a config file (that contains subscription details) and running the CHCCLP command. The out put is then concatenated in a csv file. Once all subscription details are saved the script send a mail... (7 Replies)
Discussion started by: ab095
7 Replies

2. UNIX for Beginners Questions & Answers

Script gives error when scheduled in crontab

i have written one script which is working fine when i run it manually but giving error when i schedule it in crontab. cat SUMMARY.sh #!/bin/bash DT1=`date +%Y%m%d` echo "Off PP TT" >>summary_$DT1.txt cat ues1.txt_$DT1 >>summary_$DT1.txt cat ues2.txt_$DT1... (2 Replies)
Discussion started by: scriptor
2 Replies

3. Shell Programming and Scripting

Log file is not updating when I run shell scripts scheduled thru crontab

Hi Forum, Good Day! I have created an empty html file wtih permissoin 777 created shell script(with permission 777) , code is below. #=======================start============== . /data09/oracle/apps_st/appl/D_oraapp095.env rm -rf /home/mnp/Test_log.txt echo... (1 Reply)
Discussion started by: kartheekbk
1 Replies

4. Shell Programming and Scripting

Scheduled job not running automatically in crontab

i have a job scheduled in crontab. The problem is, it is not running automatically as per the time scheduled. But runs when executed manually. What would be the problem? Help me with this please. (6 Replies)
Discussion started by: Santhosh CJ
6 Replies

5. Shell Programming and Scripting

Script not working via crontab

Hi, I have written one script which is connecting to the the database and generating one CSV, it is running fine when i ran it manually though it is throwing any warning but CSV is generating and working fine but same script when i have configured in crontab not working and giving error, kindly... (1 Reply)
Discussion started by: ash1234
1 Replies

6. Shell Programming and Scripting

Script not working via crontab

Hi, I have written one script which is connecting to the the database and generating one CSV, it is running fine when i ran it manually though it is throwing any warning but CSV is generating and working fine but same script when i have configured in crontab not working and giving error, kindly... (6 Replies)
Discussion started by: ash12345
6 Replies

7. UNIX for Dummies Questions & Answers

Script is not longer working in the crontab

This is the crontab it is supossed to be running everyday but it didnt 5 0 * * * /export/app/CO/opge/scr/Informe_parametros_colombia.ksh >/dev/null 2>&1 Inside the above script connects to a database and extract data to a flat file, manually i run the script at about 2 a.m. and Works OK,... (6 Replies)
Discussion started by: alexcol
6 Replies

8. Shell Programming and Scripting

Script not working in crontab

Hi I have created a script. Which i have configured in cron to make it run in every 2 min. But script is not running. If I checl the cron log at /var/cron/log .it shows its running in every 2 min. Below is the command which i configured in crontab. But I am not able to find the way so that... (9 Replies)
Discussion started by: nandan8a
9 Replies

9. UNIX for Dummies Questions & Answers

crontab scheduled details required

Hi, I have the below job scheduled in crontab. Can you please tell me at what interval this job is scheduled? Where is the name of the job defined here? I will definitely go through the UNIX documentation a little later, for the time being, please provide me few details on this. Thank you. ... (0 Replies)
Discussion started by: Dev_Dev
0 Replies

10. Shell Programming and Scripting

Problem with executing a script scheduled in crontab

Hi I have written a shell script(in Solaris) in which following logic is there..... i=1 while read control do key=`echo $control | awk -F$DELIMITOR '{ print $1 }'` echo "Key Values" ${key} i=`/usr/bin/expr $i + 1` done < $CONFPATH/$CONFFILE when i execute it at prompt it... (4 Replies)
Discussion started by: Amardeep
4 Replies
Login or Register to Ask a Question