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 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 |
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 & |
Thanks very much Chris!
The code is really cool now. |
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 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. Regards, |
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. |
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:
... 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. |
Quote:
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 |
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`; I have entry in Cron to start it periodically and dropped that shell script for checking and starting. Everything seems good. Regards, |
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 |
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.
|
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"
|
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. |
More simple
You can use:
ps -C program_name | grep program_name Good luck! |
All times are GMT -5. The time now is 10:56 PM. |