Shell script, for control of a process.

Hello,

I need a script to monitor that it's running only one instance of a process, and that if it's not running, then launch that process.


Thanks in advance
VMWAREit security administratorAsked:
Who is Participating?
 
sweetfa2Connect With a Mentor Commented:
#!/bin/bash
cnt=`pgrep $1 | wc -l`
if [ $cnt -ne 1 ];
then
   commandlinetostartprocess
fi

Open in new window


You need to run this process with the pattern of the command you want to find.

Your best bet is to add it to crontab and run every minute, unless required to check more frequently.
0
 
VMWAREit security administratorAuthor Commented:
Hello sweetfa2,

Could you explain me a little of your shell script?..

The process is this: /usr/bin/php -q /var/www/AFMNew/serviceFtp.php

Thanks
0
 
tel2Connect With a Mentor Commented:
Hi VMWARE,

sweetfa2's script looks reasonable to me, except, I would suggest you change:
>  if [ $cnt -ne 1 ];
to:
    if [ $cnt -eq 0 ]
because:
- If 2 or more of your processes somehow get to be running at the same time, the above script would keep starting more, until the system is overloaded.
- The ';' at the end of the line is unnecessary.

BTW, when you say "only one instance", do you want extra instances killed, or what?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
VMWAREit security administratorAuthor Commented:
Hello tel2,

"Only one instance" = Only one process. Means that's not necessary relaunch the process once again...



0
 
sweetfa2Connect With a Mentor Commented:
Line 1: tell the shell what interpreter to use
Line 2: Execute the command 'pgrep' which searches the process list for a pattern.  $1 is the parameter you put on the command line when you run it.  The output of pgrep is piped to 'wc' which is asked to count the lines.  The result is put into a variable 'cnt'
Line 3:  Check if the 'cnt' from line 2 value is not equal to 1.   If it is not equal execute line 4.  tel2 is correct in the chance that you already have more so you really should use $cnt -lt 1 which would only start if the count is less than 1.
Line 4: part of the if..then..else syntax
Line 5: /usr/bin/php -q /var/www/AFMNew/serviceFtp.php
Line 6: finish the if..then..else syntax


If you saved the script as boris.sh you would run it as

./boris.sh php

However, if you have other php processes running this may cause you issues.

You should also change the permissions on the script before trying to run it.

chmod 770 boris.sh
0
 
VMWAREit security administratorAuthor Commented:
what issues?, and how do i for avoid that issues?
0
 
TintinCommented:
I'd do it like this
#!/bin/bash
cmd="/usr/bin/php -q /var/www/AFMNnew/serviceFtp.php"

while true
do
  pgrep -f "$cmd" >/dev/null || $cmd
    sleep 60  # Adjust to whatever period you want to check
done

Open in new window

0
 
tel2Commented:
But since Tintin's script doesn't appear to be designed to be run as a cron job, if it every dies for any reason, then your FTP script no longer has life insurance.
0
 
sweetfa2Connect With a Mentor Commented:
Whilst the -f option is a good choice I would be adding it to crontab.  


crontab -e

add the line

* * * * * pathtoboris/boris.sh

:wq

In that way you don't have to worry that your monitoring script is running as well and it automatically checks every minute.
0
 
VMWAREit security administratorAuthor Commented:
This code would be correct?.

 
#!/bin/bash
cnt=`pgrep $1 | wc -l`
if [ $cnt -eq 0 ]
then
   /usr/bin/php -q /var/www/AFMNnew/serviceFtp.php
fi

Open in new window

0
 
tel2Connect With a Mentor Commented:
Hi VMWARE,

That depends on the exact argument you plan to pass to the script.  Tintin's suggested "-f" switch helps there.

I suggest you hard-code the argument (as Tintin has), so you don't have to pass it in your crontab.  I assume it's not going to be changing often?  Something like this:

If you want to convert Tintin's script to a cron job, you could do something like this:

#!/bin/bash
cmd="/usr/bin/php -q /var/www/AFMNnew/serviceFtp.php"
pgrep -f "$cmd" >/dev/null || $cmd

And use sweetfa2's cron entry, if you want it run minutely.

BTW, how often do you want the check run?
0
 
VMWAREit security administratorAuthor Commented:
Hello tel2,

I want to check hourly...
0
 
tel2Connect With a Mentor Commented:
Hi WMWARE,

If you want hourly checks, you'll probably want a cron entry that looks something like this:

    0 * * * * pathtoboris/boris.sh

But if you want to see how often this script is restarting the process (I would), you might like to have a cron entry something like this:

    0 * * * * pathtoboris/boris.sh >/tmp/boris.log 2>&1

and change the script (boris.sh) to something like this:

    #!/bin/bash
    cmd="/usr/bin/php -q /var/www/AFMNnew/serviceFtp.php"
    pgrep -f "$cmd" >/dev/null || date && $cmd

which should put the date & time into boris.log each time the process is restarted.  It should also put any errors from boris.sh into boris.log, whether the process is restarted or not.
(I have not tested the above, so it may have bugs.)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.