Aix shell script question


 
Thread Tools Search this Thread
Operating Systems AIX Aix shell script question
# 1  
Old 03-31-2010
Aix shell script question

Hi,

When I run the following script from a shell it works fine as root. But when I try to run from cron as root it fails everytime? I set the scripts I run in cron to send me the output if they fail but this produces nothing. Please see script below, maybe someone can see whats wrong as I'm sure it's easy for someone who knows what there doing :-)

Code:
#/usr/bin/ksh!
DATE=`date`
HOST=`hostname`
LOGGER=/logger
MAIL2=matt@test.com
RESULTS=/home/matt/log/logresults
 
mail -s "Weeekly command history on $HOST $DATE" "$MAIL2" < $LOGGER
 
sleep 10
 
ps -ef | sed -n '/logger/{/grep/!p;}' | awk '{print$2}' | xargs -i kill {}
if [ $? == "0" ]
then
echo "Logger killed on $HOST" > $RESULTS
else
echo "Logger proceess not killed on $HOST please investigate" | mail -s "Logger process for Loggermail failed on $HOST $DATE" "$MAIL2"
exit 1
fi
 
sleep 10
 
/home/matt/log/loggernohup
if [ $? == "0" ]
then
echo "Logger process has been started on $HOST" >> $RESULTS
else
echo "Logger has not been restarted on $HOST please investigate" | mail -s "Logger process s not been restarted on $HOST $DATE" "$MAIL2"
exit 1
fi
 
sleep 120
 
echo > /logger
 
echo | mail -s "logger restarted successfully $HOST $DATE" "$MAIL2" < $RESULTS


Last edited by pludi; 03-31-2010 at 06:43 AM.. Reason: code tags, please...
# 2  
Old 03-31-2010
Running a script from cron is not the same like when being run with a user from the prompt. There are several reasons like paths that are not set and other environment variables that might be missing, since a cron job does not source a .profile and other files by default like a normal log in does by default.

Do you write a log for your script ie. something like
Code:
* * * * * /mydir/myscript.ksh >> /mydir/myscript.log 2>&1

Also check your mails for the failed cronjob and have a look into /var/adm/cron/log for any suspicous entries regarding your script.

Here is a detailed explanation (check last paragraph):
https://www.unix.com/answers-frequent...n-crontab.html

---------- Post updated at 03:12 PM ---------- Previous update was at 12:29 PM ----------

After having a look again I just noticed that the shebang has a syntax error:
Code:
#/usr/bin/ksh!

should be
Code:
#!/usr/bin/ksh

# 3  
Old 04-07-2010
Cheers for the quick response.

As you can probably tell i'm abit of a newbie to scriptng. After changing the shell to "#!/usr/bin/ksh it started to fail again when running interactively. It came up with Terminating to my screen as if the script was being killed. Which it was by the "ps" so I changed it to he following
Code:
ps -ef | sed -n '/'"logger"'/{/grep/!p;}' | grep -v loggermail | awk '{print$2}' | xargs -i kill {}

Thanks

Matt

Last edited by bakunin; 04-07-2010 at 09:04 AM.. Reason: donated some code-tags to the charitable cause ;-)
# 4  
Old 04-07-2010
Quote:
Originally Posted by elmesy
Code:
ps -ef | sed -n '/'"logger"'/{/grep/!p;}' | grep -v loggermail | awk '{print$2}' | xargs -i kill {}

I am not sure if this does what it should do:

Code:
sed -n '/logger/{/grep/!p;}'

This (takes the output of "ps -ef" and) prints all the lines containing "logger" except those which contain "grep" too. As far as i can see there is no grep process running up to this point. Probably you filtered the lines with "grep" before but switched to "sed". You should change the command accordingly to:

Code:
sed -n '/logger/{/sed/!p;}'

In this case you could also skip the next command where you filter out "loggermail", use sed for awk's task and combine this to one command, arriving at the following line:

Code:
ps -ef | sed -n '/logger[^m]/{/sed/d; s/^ *[^ ]*  *\([^ ]*\) .*$/\1/p;}' | xargs -i kill {}

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script in AIX

Hello Experts My SAP application is running in IBM AIX. Everyday my application generate some files in the below path as below. root@sedcaspm0502: pwd /interfaces/RFTS/B11/archive root@sedcaspm0502: ls -lrt -rw-r r 1 ppgadm sapsys 1039445 May 08 01:20... (1 Reply)
Discussion started by: sundar.c79@gmai
1 Replies

2. Shell Programming and Scripting

Want to Schedule Shell Script on AIX

Hi All, I want to schedule a shell script on AIX. But Crontab is not working in my AIX Server.Is there any alternative? Please Suggest.. (2 Replies)
Discussion started by: bharat1211
2 Replies

3. Shell Programming and Scripting

Need help in shell script on AIX

Hi All, I am connecting from ServerA(Unix) to ServerB (AIX). Copying a Database dump file from A to B. On B i need to import that dump file. I have used the following code to do this operation but ended up with error Pseudo-terminal will not be allocated because stdin is not a terminal.... (2 Replies)
Discussion started by: maddyd2k
2 Replies

4. UNIX for Dummies Questions & Answers

Shell Script in AIX

Hi , Good Morning every one !!! I am very new to this forum and new to Shell Script as well , hope some script Guru's will help me . I have a requirment to write a shell script . The requirment is like this . There are couple of shell scripts scheduled in the Crontab (some scripts run... (5 Replies)
Discussion started by: dilipd21
5 Replies

5. AIX

AIX Shell script does not work

Hi. I created schell script for sending messages to some processes in AIX: #!/bin/sh BSE=/infor/ERPLN/bse BSE_TMP=/infor/ERPLN/bse/tmp export BSE BSE_TMP for i in `ps -eo pid,comm | grep bshell | cut -f 1 -d " "` do /something $i done Unfortunatelly this script does not work on... (6 Replies)
Discussion started by: giovanni
6 Replies

6. Shell Programming and Scripting

shell script on AIX

Hi! I have made a simple script for writing the following line on a file (file.txt): list= 1 2 3 and thr script looks like this: N=3 ll=(1 2 3) echo -n "list= ">> file.txt j=0 while ; do echo -n ${ll}" ">> file.txt let j++ done The code works fine on Linux,... (3 Replies)
Discussion started by: carl.alv
3 Replies

7. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

8. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies

9. Shell Programming and Scripting

porting shell script from Linux to AIX.

Hi, I am porting one shell script from Linux to AIX. I had .ksh file and i have changed it to .sh file for aix. on linux this script is running fine but on aix it gives me "unexpected end of file" error. Could any one suggest me what to do to port this script error free? Thanks in... (6 Replies)
Discussion started by: joy_1
6 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question