Solved

Shell script, for control of a process.

Posted on 2010-11-29
13
511 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:VMWARE
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 250 total points
ID: 34235370
#!/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
 

Author Comment

by:VMWARE
ID: 34235423
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
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 34235426
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:VMWARE
ID: 34235452
Hello tel2,

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



0
 
LVL 17

Assisted Solution

by:sweetfa2
sweetfa2 earned 250 total points
ID: 34235456
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
 

Author Comment

by:VMWARE
ID: 34235478
what issues?, and how do i for avoid that issues?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34235500
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
 
LVL 12

Expert Comment

by:tel2
ID: 34235522
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
 
LVL 17

Assisted Solution

by:sweetfa2
sweetfa2 earned 250 total points
ID: 34235527
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
 

Author Comment

by:VMWARE
ID: 34235552
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
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 34235633
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
 

Author Comment

by:VMWARE
ID: 34242073
Hello tel2,

I want to check hourly...
0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 34242815
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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