There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
UNIX/Linux
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > UNIX/Linux >
I need some help in this unix script


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Saiprabhu's Avatar
Junior Member with 2 posts.
 
Join Date: Nov 2004
Experience: Intermediate
17-Nov-2004, 09:06 PM #1
I need some help in this unix script
Basically there are three scripts, for example walk.sh, stand.sh and sleep.sh
and the walk.sh should not run when either stand.sh or sleep.sh is running.
for this I have put in walk.sh as below:
#!/bin/ksh

sleeppid=`ps -aef | grep -i "sleep.sh" | grep -v grep | awk '{print $2}'`
standpid=`ps -aef | grep -i "stand.sh" | grep -v grep | awk '{print $2}'`
if [ ${sleeppid} -gt 1 ] || [ ${standpid} -gt 1 ]
then
echo "sleep or stand process already started"
exit
else
echo "walk process can be started"
sleep 120

when I ran the walk.sh script I got below error message,
but when sleep or stand scripts are running, and when I tried to start
walk script I got correct message as "sleep or stand process already started"
I tried hard I did't able to correct this walk.sh script,
It is saying about test argument , which I dont understand
please help as early as possible:
=======================================
ksh -x walk.sh
+ + ps -aef
+ grep -i sleep.sh
+ awk {print $2}
+ grep -v grep
sleeppid=
+ + ps -aef
+ grep -i stand.sh
+ awk {print $2}
+ grep -v grep
standpid=23104
+ [ -gt 1 ]
walk.sh[5]: test: argument expected
+ [ 23104 -gt 1 ]
+ echo sleep or stand process already started
sleep or stand process already started
+ exit
==================================
Squashman's Avatar
Distinguished Member with 12,604 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
17-Nov-2004, 09:25 PM #2
Is that the whole walk.sh. script? I would assume you need to close the IF statement. But I do not know if scripting in KSH is different from Bash. I have never used KSH.

if
fi
Saiprabhu's Avatar
Junior Member with 2 posts.
 
Join Date: Nov 2004
Experience: Intermediate
17-Nov-2004, 09:37 PM #3
Quote:
Originally Posted by LwdSquashman
Is that the whole walk.sh. script? I would assume you need to close the IF statement. But I do not know if scripting in KSH is different from Bash. I have never used KSH.

if
fi
Thanks a for fast response,
I closed the script with fi but I missed this while pasting this script into this thread , I don't think there will be much difference between ksh and bash with regard to below logic.

#!/bin/ksh

sleeppid=`ps -aef | grep -i "sleep.sh" | grep -v grep | awk '{print $2}'`
standpid=`ps -aef | grep -i "stand.sh" | grep -v grep | awk '{print $2}'`
if [ ${sleeppid} -gt 1 ] || [ ${standpid} -gt 1 ]
then
echo "sleep or stand process already started"
exit
else
echo "walk process can be started"
sleep 120
fi
Squashman's Avatar
Distinguished Member with 12,604 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
17-Nov-2004, 10:49 PM #4
I think I know what the problem is. If sleeppid or standpid = blank because they are not running. The comparison is not made because the value of sleeppid or standpid is not an integer.

At the beginning of your script, try setting sleeppid and standpid to 0
sleeppid=0
standpid=0
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User
Squashman's Avatar
Distinguished Member with 12,604 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
18-Nov-2004, 09:55 AM #5
Got up this morning and realized that my previous post will not work either, but I was on the right track. You need to test if the script is running.

Code:
if ps -ef | grep "sleep.sh" | grep -v grep >/dev/null then
echo "sleep process already started"
exit
else
echo "walk process can be started"
sleep 120
fi
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:10 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.