ksh behavior change on RHEL5


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh behavior change on RHEL5
# 1  
Old 04-27-2010
Question ksh behavior change on RHEL5

I recently patched a RHEL5 host from 5.2 to 5.5. There are some scripts that simply do a ps and grep to check for itself before proceeding. I personally don't like the way the scripts are written and would have done it differently. Before I can proceed with patching the production servers, I need to determine why the behaviour changed with these scripts. I've provided a sample script that reproduces the issue and output from both the patched system and a none patched system. Running the script manually everything works as expected, but these run through cron and give different output.

A small oddity I just noticed too, when type 'cat /etc/redhat' and hitting tab on the patched system you can see below that it put a '\' before the '-', but on the unpatched system it does not.

Crontab:
Code:
* * * * * /app2/stage/aim/aim_ap_etl/test.ksh > /app2/stage/aim/aim_ap_etl/test.out 2>&1

Script:
Code:
#!/bin/ksh

echo $0
SCRIPT=${0##*/}
echo $SCRIPT
echo "IFS=xx${IFS}xx"
echo "PS=$(which ps)"
echo "GREP=$(which grep)"
echo "PGREP=$(which pgrep)"
echo "WC=$(which wc)"
echo "Running processes: "
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT
/bin/ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT | /usr/bin/wc -l
/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l
echo "-----------"

proc1=`ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT`
echo "proc1=$proc1"

proc2=$(ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT)
echo "proc2=$proc2"

proc3=$(/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l)
echo "proc3=$proc3"

echo "Exiting!"
exit

Non-patched
Code:
> uname -a
Linux xxx 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64

> cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.2 (Tikanga)

> echo ${.sh.version}
Version M 1993-12-28 r

Manual output with ksh:
Code:
> ./test.ksh
./test.ksh
test.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      2365 24775  0 12:28 pts/2    00:00:00 /bin/ksh ./test.ksh
1
1
1
-----------
proc1=1
proc2=1
proc3=1
Exiting!

Manual output with bash:
Code:
> ./test.bash
./test.bash
test.bash
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      2914 24775  0 12:30 pts/2    00:00:00 /bin/bash ./test.bash
1
1
1
-----------
proc1=2
proc2=2
proc3=2
Exiting!

Crontab output with ksh:
Code:
> cat test.out
/app2/stage/aim/aim_ap_etl/test.ksh
test.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      3823  3820  0 12:36 ?        00:00:00 /bin/sh -c /app2/stage/aim/aim_ap_etl/test.ksh > /app2/stage/aim/aim_ap_etl/test.out 2>&1
infa      3824  3823  0 12:36 ?        00:00:00 /bin/ksh /app2/stage/aim/aim_ap_etl/test.ksh
2
2
1
-----------
proc1=2
proc2=2
proc3=1
Exiting!

Patched
Code:
> uname -a
Linux xxx 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

> cat /etc/redhat\-release                                                                               
Red Hat Enterprise Linux Server release 5.5 (Tikanga)

> echo ${.sh.version}
Version AJM 93t+ 2010-02-02

Manual output with ksh:
Code:
> ./test.ksh
./test.ksh
test.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      3288 11352  0 12:27 pts/3    00:00:00 /bin/ksh ./test.ksh
1
1
1
-----------
proc1=2
proc2=2
proc3=2
Exiting!

Manual output with bash:
Code:
> ./test.bash
./test.bash
test.bash
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      3702 11352  0 12:28 pts/3    00:00:00 /bin/bash ./test.bash
1
1
1
-----------
proc1=2
proc2=2
proc3=2
Exiting!

Crontab output with ksh:
Code:
> cat test.out
/app2/stage/aim/aim_ap_etl/test.ksh
test.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes: 
infa      3962  3961  0 12:32 ?        00:00:00 /bin/sh -c /app2/stage/aim/aim_ap_etl/test.ksh > /app2/stage/aim/aim_ap_etl/test.out 2>&1
infa      3963  3962  0 12:32 ?        00:00:00 /bin/ksh /app2/stage/aim/aim_ap_etl/test.ksh
2
2
1
-----------
proc1=3
proc2=3
proc3=2
Exiting!

# 2  
Old 04-27-2010
Can you log and report to us what value ksh thinks the SCRIPT variable has when you run the cron job?
# 3  
Old 04-27-2010
It's the second value echo'd in the script.

echo $SCRIPT

test.ksh

---------- Post updated at 13:24 ---------- Previous update was at 13:21 ----------

Also, any idea why bash outputs a different value than ksh? Most of the values are 2 instead of 1.
# 4  
Old 04-27-2010
MySQL

I run script consecutively for testing..

Code:
[root@rhnserver ~]# ./script
./script
script
IFS=xx
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes:
root     32647 30389  0 05:03 pts/0    00:00:00 /bin/ksh ./script
1
1
1
-----------
proc1=1
proc2=1
proc3=1
Exiting!
[root@rhnserver ~]# ./script
./script
script
IFS=xx
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
Running processes:
root     32672 30389  0 05:03 pts/0    00:00:00 /bin/ksh ./script
1
1
1
-----------
proc1=1
proc2=2
proc3=1
Exiting!

The result is different,because it works same time cron Smilie
(let think the cron every minute works )
And cron is seeing both your script both bash and ksh

Try diffrent process for example
"script=sshd"
Must be no different the output Smilie
# 5  
Old 04-27-2010
The cron was not running at the same time. I was done afterwards. The value expected from cron is 2 because cron spawns the script using Bourne shell (sh) as seen from the output.

infa 3962 3961 0 12:32 ? 00:00:00 /bin/sh -c /app2/stage/aim/aim_ap_etl/test.ksh > /app2/stage/aim/aim_ap_etl/test.out 2>&1
infa 3963 3962 0 12:32 ? 00:00:00 /bin/ksh /app2/stage/aim/aim_ap_etl/test.ksh

The primary issue that I'm dealing with is that the developers who created a script that runs on these hosts are producing different output since the patching. As you can see, the patched system is assigning the value of 3 when it should be 2 to the proc variables when run via cron.
# 6  
Old 04-27-2010
MySQL

Quote:
Originally Posted by bwhitehd
The cron was not running at the same time. I was done afterwards. The value expected from cron is 2 because cron spawns the script using Bourne shell (sh) as seen from the output.

infa 3962 3961 0 12:32 ? 00:00:00 /bin/sh -c /app2/stage/aim/aim_ap_etl/test.ksh > /app2/stage/aim/aim_ap_etl/test.out 2>&1
infa 3963 3962 0 12:32 ? 00:00:00 /bin/ksh /app2/stage/aim/aim_ap_etl/test.ksh

The primary issue that I'm dealing with is that the developers who created a script that runs on these hosts are producing different output since the patching. As you can see, the patched system is assigning the value of 3 when it should be 2 to the proc variables when run via cron.
I dont think a cron related problem..Also manuel outputs is giving same result that add one more to variables on patched systems..
Maybe when your ps value send to variable kernel show another process that named called script..

Maybe
Code:
root     14760 14747  0 06:43 pts/1    00:00:00 [script]

And you try probably `ps aux` for clear result..

Or you must put same lines after variables again in your code
Again lets see output Smilie


Code:
 
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT | /usr/bin/wc -l

echo "-----------"
 
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT
proc1=`ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT`
echo "proc1=$proc1"
 
/bin/ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT
proc2=$( (ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT) )
echo "proc2=$proc2"
 
/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l
proc3=$(/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l)
echo "proc3=$proc3"

/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT | /usr/bin/wc -l

# 7  
Old 04-27-2010
You're correct, cron is not the issue specifically. It's the fact that the assigned value is one more than it should be.

Code:
#!/bin/ksh

echo $0
SCRIPT=${0##*/}
echo $SCRIPT
echo "IFS=xx${IFS}xx"
echo "PS=$(which ps)"
echo "GREP=$(which grep)"
echo "PGREP=$(which pgrep)"
echo "WC=$(which wc)"

/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT | /usr/bin/wc -l

echo "-----------"
 
/bin/ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT
proc1=`/bin/ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT`
echo "proc1=$proc1"
 
echo "-----------"
/bin/ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT
proc2=$( (ps -ef | /bin/grep -v grep | /bin/grep -c $SCRIPT) )
echo "proc2=$proc2"
 
echo "-----------"
/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l
proc3=$(/usr/bin/pgrep $SCRIPT | /usr/bin/wc -l)
echo "proc3=$proc3"

echo "-----------"
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT
/bin/ps -ef | /bin/grep -v grep | /bin/grep $SCRIPT | /usr/bin/wc -l

output from patched
Code:
> ./test2.ksh
./test2.ksh
test2.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
brian     3728   553  0 15:48 pts/8    00:00:00 /bin/ksh ./test2.ksh
1
-----------
1
proc1=2
-----------
1
proc2=2
-----------
1
proc3=2
-----------
brian     3728   553  0 15:48 pts/8    00:00:00 /bin/ksh ./test2.ksh
1

output from non-patched
Code:
> ./test2.ksh
./test2.ksh
test2.ksh
IFS=xx 
xx
PS=/bin/ps
GREP=/bin/grep
PGREP=/usr/bin/pgrep
WC=/usr/bin/wc
brian    11485 10981  0 15:50 pts/1    00:00:00 /bin/ksh ./test2.ksh
1
-----------
1
proc1=1
-----------
1
proc2=1
-----------
1
proc3=1
-----------
brian    11485 10981  0 15:50 pts/1    00:00:00 /bin/ksh ./test2.ksh
0

The last number of the non-patched system is incorrect here. "0" should be "1"
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh behavior in scripts spawned w/nohup

I have a need to run any number of identical scripts simultaneously, so I've created a driver script which reads a template script, edits these appropriately and then submits them via nohup. The spawned scripts all check to see at some point how many of their number are running and once the count... (7 Replies)
Discussion started by: safedba
7 Replies

2. Shell Programming and Scripting

change output format using ksh

I have a script that reaches out to several systems and pulls back infomation in serveral files. I would like to take the infomation returned and reformat it so I can export it to excel. Below is current output: File1:item1:abc=1 File1:item2:efg File2:item1:ab=1 File2:item2:efg... (3 Replies)
Discussion started by: oldman2
3 Replies

3. Shell Programming and Scripting

ksh script to change passwd

Hello All, I am trying to change a user passwd (one time password): cat /tmp/passwd mnop1234 mnop1234 #passwd abcd < /tmp/passwd (for some reason, it is not able to input the password from /tmp/passwd and comes back with "New Password: ") Is there a work around except using "expect". ... (1 Reply)
Discussion started by: solaix14
1 Replies

4. UNIX for Advanced & Expert Users

How to change the permissions of ksh?

Suppose, I want to execute unix commands. For that I have to go to ksh, but if I don't have execute permission to ksh itself then is there any way to change the permission of ksh? chmod command does not work for this because, I don't have permission to ksh itself... :) Let me know, if you have... (3 Replies)
Discussion started by: ponnuvel
3 Replies

5. UNIX for Dummies Questions & Answers

How can I change the behavior of the mouse in Terminal?

Hi, I hope I'm posting in the correct forum, so here goes. I would like for the mouse right/left click to work in Terminal (OSX) as it does in Putty. For instance, when I double click 'log' in file.log.gz, only the log part is highlighted (in Terminal), while in Putty the entire file name is... (0 Replies)
Discussion started by: TheChemist
0 Replies

6. UNIX for Dummies Questions & Answers

Change to ksh shell

Hi everyone: Mi default shell is sh, i need to set the TMOUT variable so i need to change the shell to ksh, I change the /etc/profile file as follows, but when i do an echo $0 stll sh been the shell: # more /etc/profile # # (c) Copyright 1990, OPEN SOFTWARE FOUNDATION, INC. # ALL RIGHTS... (2 Replies)
Discussion started by: irasela
2 Replies

7. Shell Programming and Scripting

Strange behavior from 'read' statements. (ksh - but could be same on other shells)

I'm getting rather frustrated with an interactive script I'm writing. The script is divided up, with section for setting variable at the top, then functions (which make up most of the script) then basically a line at the end which calls the first function- the program moves between the... (5 Replies)
Discussion started by: alexop
5 Replies

8. Shell Programming and Scripting

ksh parameter --- change

I am reading a file into a parameter in my program typeset nums_type if the contents of nums_type = asdfghbqwerty how do make a new paremeter eqaul to the 7th character of nums_type. like ${type} = b (2 Replies)
Discussion started by: frank
2 Replies
Login or Register to Ask a Question