Shell script to start another program, if not running.
Linux - SoftwareThis 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.
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.
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.
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.
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.
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.
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
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:
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.