Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

unix shell question - #!/bin/sh

Posted on 2002-03-27
9
Medium Priority
?
359 Views
Last Modified: 2010-04-21
I need to check if a perl script is already running.

If it is i need to kill it and re-run the script.

I'm using #!/bin/sh.

How do i do this?

regards
Peewee
0
Comment
Question by:Peewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 5

Author Comment

by:Peewee
ID: 6898977
In addittion if a perl script is not running and i try and kill it then dont try and kill it, just tell me it's not running..

cheers
Peewee
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6899270
Hi  Peewee,

    Let check myscript is runing or not: (the script name: myscript, you can replace it to whatever you want)

#======================================================

  Here's a scripe to check if the your script is running:
#
#!/bin/sh
     pid=`ps -ef | grep myscript | grep -v grep | awk '{print $2} ' `        
     if test "$pid" ;  then
          echo " The myscript is runing"
     else
            echo "No PID. myscript is not  running"
   
     fi
exit 0
#================================================

0
 
LVL 5

Author Comment

by:Peewee
ID: 6900031
yuzh,

    pid=`ps -ef | grep my_perl.pl | grep -v grep | awk '{print $2} ' `      

this does not seem to work. i.e. the script is running but the line cannot find it..

can u fix it?

regards
Peewee
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Peewee
ID: 6900045
yuzh,
oh in addittion can you cater for multiple instances of the same script running ie the ps -ef  returns an array of pids.

regards
Peewee
0
 
LVL 38

Accepted Solution

by:
yuzh earned 200 total points
ID: 6901014
Which favour of UNIX are you using? may be you script have
completed before you run the script.

To check for multiple instances of the same script runing,
you can put all the PID in one text file.


#!/bin/sh


ps -ef | grep my_perl.pl | grep -v grep | awk '{print $2} > myscript.pid

if [ -s myscript.pid ] ; then
   echo " there are `cat myscript.pid | wc -l` of  my_perl.pl running "

else
   echo "my_perl.pl is not runing"
fi

#=========================================================

   BTW: could you please post a output of:
   ps -ef | grep loginname

   for me
0
 
LVL 5

Author Comment

by:Peewee
ID: 6902162
yuzh,
oh in addittion can you cater for multiple instances of the same script running ie the ps -ef  returns an array of pids.

regards
Peewee
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6902346
all the pids are stored in the file myscript.pid
you can out them into one variable: eg
PIDS=`cat myscript.pid`
read them into an array if you wish.
all you need to do is to modify the script the way you wanted.
0
 
LVL 5

Author Comment

by:Peewee
ID: 6902375
yuzh,
oh in addittion can you cater for multiple instances of the same script running ie the ps -ef  returns an array of pids.

regards
Peewee
0
 
LVL 5

Author Comment

by:Peewee
ID: 6902380
yuzh,
aplogies i've done a couple of refreshes and it seems to have reposted  old info..

solution was good to go...

many thanks
Peewee
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question