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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-04-2008, 05:09 AM
|
#1
|
Member
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62
Rep:
|
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
|
|
|
03-04-2008, 05:34 AM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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 &
|
|
|
03-04-2008, 07:28 AM
|
#3
|
Member
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62
Original Poster
Rep:
|
Thanks very much Chris!
The code is really cool now.
|
|
|
03-04-2008, 07:35 AM
|
#4
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Rep:
|
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.
|
|
|
03-04-2008, 11:40 PM
|
#5
|
Member
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62
Original Poster
Rep:
|
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.
|
|
|
03-05-2008, 12:13 AM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
03-05-2008, 05:34 AM
|
#7
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Rep:
|
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
|
|
|
03-05-2008, 06:54 AM
|
#8
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,508
Rep:
|
Quote:
Originally Posted by cooljai
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
|
|
|
03-05-2008, 11:17 PM
|
#9
|
Member
Registered: May 2007
Location: /dev/random
Distribution: CentOS, Fedora, RHEL, SuSE
Posts: 62
Original Poster
Rep:
|
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,
|
|
|
07-20-2011, 02:45 AM
|
#10
|
LQ Newbie
Registered: Jul 2011
Posts: 5
Rep: 
|
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.
|
|
|
07-20-2011, 06:56 PM
|
#11
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
02-24-2012, 05:28 AM
|
#12
|
LQ Newbie
Registered: Oct 2010
Posts: 1
Rep:
|
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"
|
|
|
02-24-2012, 05:44 AM
|
#13
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
03-13-2012, 04:39 AM
|
#14
|
LQ Newbie
Registered: Mar 2012
Location: Cherbourg, France
Distribution: Arch, Slackware
Posts: 1
Rep: 
|
More simple
You can use:
ps -C program_name | grep program_name
Good luck!
|
|
|
All times are GMT -5. The time now is 09:49 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|