LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-04-2008, 05:09 AM   #1
cooljai
Member
 
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62

Rep: Reputation: 15
Question Shell script to start another program, if not running.


Hi,

I want a shell script which check whether a certain program is running or not, if not, then start it. There are two programs for which I need this. I have made this script and setup it in cron to execute it every hour for checking/starting of my programs:
first program name: logsUpdate.pl
Second program name: ProcessFetched.pl

Code:
#!/bin/bash

`ps ax | grep "[l]ogsUpdate.pl" > tmpCheckMgr`

if [ ! -s tmpCheckMgr ]
then
`/root/logsUpdate.pl` &
fi

`ps ax | grep "[P]rocessFetched" > tmpCheckLog`

if [ ! -s tmpCheckLog ]
then
`/root/ProcessFetched.pl now` &
fi
This script is working fine. It start the programs if they are not running but the problem is that it itself remain in memory (I can see it in ps list). I just want that this script will execute the programs and exit.

you may wonder why I'm using ps...> file to check program in process list. I also tested using if [`ps ax | grep ...` ], but it didnt came with desired result, that's why I've used this unusual code.

Can anybody please point me to right direction?

Thanks
 
Old 03-04-2008, 05:34 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
A few comments:

1. Instead of temp files, use a var eg:

log_found=`ps -ef|grep -v grep|grep gnome-terminal|awk '{print $8}'`

using grep -v grep to avoid picking up the grep process itself

2. backquotes redundant here

`/root/logsUpdate.pl` &

3. to detach from the process, use nohup eg:

nohup /root/logsUpdate.pl &
 
Old 03-04-2008, 07:28 AM   #3
cooljai
Member
 
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62

Original Poster
Rep: Reputation: 15
Thanks very much Chris!

The code is really cool now.
 
Old 03-04-2008, 07:35 AM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
May be you can make the things a bit simpler.

Start yours programs as cronjobs, as usual, as recurrent jobs (i mean, every n minutes/hours it is up to you). This solves the problem they are not running. There is no need to check, just put it to run again every n minutes.

To avoid 2 or more instances of the same program running at the same time, use a lock.
Change your programs to add at the very beginning, something like that:

Code:
#!/bin/bash

unalias -a
trap onexit SIGINT SIGSEGV SIGQUIT SIGTERM

prog="yourprogramname"
lock="/tmp/${prog}.lock"

onexit () {
        rm -f "${lock}"
        exit
}

# check if the lock file is in place.
if [ -f $lock ]; then
        # silent exit is better from cron jobs,
        # echo "$0 Error: Lock file $lock is in place."
        # echo "Make sure an old instance of this program is not running, remove i
t and try again."
        exit
fi
date > $lock

#
# your script goes here
#
   
# 
# exit your program calling onexit
#

onexit
In this way, if another instance is already running when the second one try to start, the check in the beginning will terminate the second instance, avoid 2 or more programs at the same time. When the first one ends, it removes the lock and another one can start.

The programs are independent and each one has its own lock.
 
Old 03-04-2008, 11:40 PM   #5
cooljai
Member
 
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62

Original Poster
Rep: Reputation: 15
Thanks buddy for your valuable input.
.
As I need to run my scripts periodically but one of them is 'closed' source compiled script which, of course, I can not change. But it seems the script exit itself automatically when its instance already running.
.
The second script is in perl. I can modify it to suit the setting. Can you suggest a perl approach equivalent to your description above in shell script, means how to implement that "trap..." stuff in perl? or should I just go ahead with:
1) check lock when perl script starts, exit if lock is there.
2) if lock not there, create the lock file and continue.

That seems okay, but I'm a bit confused as what happened when my script get terminated in middle? (I can terminate it or system can restart), So plz advise in this regard.

Regards,

Last edited by cooljai; 03-04-2008 at 11:41 PM.
 
Old 03-05-2008, 12:13 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Y, that lockfile stuff is broken if the prog terminates in the middle. If I need to do that (equiv) I either parse eg a 'ps -ef|grep -v grep|grep progname' cmd (see IPC::Open2 for example) or, even easier if the schedule is regular-ish, consider making a daemon of the job and just wait x seconds between loops, instead of cronning it.
You can also add a small watchdog shell script in cron in case the daemon fails.
Just depends on how critical the job is and whether you'd notice anyway if it failed, through other means eg no files appearing somewhere.
 
Old 03-05-2008, 05:34 AM   #7
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Hi !

I think you could use the script I suggest as is, without modifying your scripts.
Just call your original script bellow "your script goes here". When your script ends for any reason, the call to "onexit" still is evaluated, no matter what language your original program is written.

Code:
...
#
# your script goes here
#

/root/logsUpdate.pl

# 
# exit your program calling onexit
#

onexit
This is a kind of wrapper that make your original program aware of simultaneous execution.

But you really need to try. May be there are others factors we aren't seeing right now that could mess with.

And as chrism01 suggested, this is not the only solution. chrism01's hint is just as good as mine. You just need to choose one that best fit to the problem and to your style of doing things.

Last edited by marozsas; 03-05-2008 at 06:03 AM. Reason: changed "Just put your.." to "Just call your" to better describe the right action
 
Old 03-05-2008, 06:54 AM   #8
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by cooljai View Post
That seems okay, but I'm a bit confused as what happened when my script get terminated in middle? (I can terminate it or system can restart), So plz advise in this regard.

Regards,
I'm sorry I forget to comment this in my previous post.

Using the wrapper your script can terminate in the middle, doesn't matter.

If your system restart in the middle, problems may arise if your system does not clean /tmp at reboot (in some system this is configurable). The lock file is in place and the next run of wrapper is blocked.

To just remove lock files at reboot, you can add the following to the root's crontab:
Code:
#min    #hour   #day    #month  #week   #command
#0-59   0-23    1-31    1-12    0-7 (0,7=Sun, names are ok)
@reboot                                 rm -f /tmp/*.lock
 
Old 03-05-2008, 11:17 PM   #9
cooljai
Member
 
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62

Original Poster
Rep: Reputation: 15
Thanks again buddy for your cool suggestion. I'll test this stuff.

Btw, I did a naughty thing. I have tested that my closed source script will exit itself if its already running. thats good. Now to avoid running my own perl script twice, I am using this code in start of script:
Code:
my $CheckRun = `ps -ef|grep -v grep|grep logsUpdate.pl|wc -l`;
if($CheckRun > 1) {
#        print "\n Already running.";
        exit;
}
I think (plz correct me if I'm wrong) that I can achieve the desired without going other bigger possible ways (like making it daemon etc.)

I have entry in Cron to start it periodically and dropped that shell script for checking and starting. Everything seems good.

Regards,
 
Old 07-20-2011, 02:45 AM   #10
ramslinux
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Database is running or not if it is running then it will mount filesystem

Hi,

can you please tell me how to write these scripts

1. i need to check whether the database is running or not if it is running it will run auto mount script.

2. For every 1 hour i need to check mount partion is mounted or not if it is not mounted it should be mounted.

Thanks & Regards
Ram

Last edited by ramslinux; 07-20-2011 at 02:48 AM.
 
Old 07-20-2011, 06:56 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Do Not resurrect a 3 yr old thr. Please start a new one instead and supply my more detail eg distro+version, what DB etc.
 
Old 02-24-2012, 05:28 AM   #12
rednectar
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
Thanks for nohup

Thanks for the tip Chris - might be an old thread but it still works Was bashing my head (no pun intended) until you showed me "nohup"
 
Old 02-24-2012, 05:44 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
No worries: using '&' without nohup is a classic error in these sorts of situations.
'&' CAN be used without nohup, but usually only when the parent is waiting on/checking the child and is anyway already 'nohup'ped at some higher level.
 
Old 03-13-2012, 04:39 AM   #14
vtudorache
LQ Newbie
 
Registered: Mar 2012
Location: Cherbourg, France
Distribution: Arch, Slackware
Posts: 1

Rep: Reputation: Disabled
More simple

You can use:

ps -C program_name | grep program_name

Good luck!
 
  


Reply

Tags
check, program, running, script, shell



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running C++ program in Linux Shell Alkibiades Programming 17 10-28-2006 06:51 AM
Shell command to start program? neilcpp Linux - Desktop 2 10-07-2006 03:53 AM
Running shell script within a C or C++ program Quantum0726 Programming 2 06-15-2005 09:14 PM
can't start program from shell roxxe1 Linux - General 11 04-24-2005 07:58 PM
running a program from shell script Suinatsa Programming 10 04-14-2005 11:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration