Solved

Shell script, for control of a process.

Posted on 2010-11-29
13
509 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now