Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

unix shell question - #!/bin/sh

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
Peewee
Asked:
Peewee
  • 6
  • 3
1 Solution
 
PeeweeAuthor Commented:
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
 
yuzhCommented:
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
 
PeeweeAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PeeweeAuthor Commented:
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
 
yuzhCommented:
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
 
PeeweeAuthor Commented:
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
 
yuzhCommented:
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
 
PeeweeAuthor Commented:
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
 
PeeweeAuthor Commented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now