Hey Listees, I have a question (hopefully quite simple) about the system V init scripts in /etc/init.d. I have an app installed and for some reason it had no system V init script. It has been installed a long while now so I can't quite remember whats going on but basically I remember I wrote the init script my self and the app is enabled as a service which chkconfig to run at system levels 3, 4 and 5. However when I wrote the script I only ever added a "start" and "stop" clause to it (why I can't remember?), now I need to add a "restart" clause but I'm having an issue. I have pasted the code below from my "restart" clause but from what I can tell, it is killing the app but not restarting it, I think because the killing process is still in action so when it start the app again it just gets killed straight away. If I enter "/etc/init.d/my_app restart" the app is terminated but does not start again, entering "/etc/init.d/my_app start" immediately after fires it up straight away so there is no problem there; can someone suggest a better way as my scripting is very begginnerish (if thats a word) and I don't doubt for a second I am doing this in a very inefficient manner: <snipety snip snip> restart) echo -n "Stopping my_app: " pgrep my_app | while read PIDS; do # I have chosen this method because my_app spawns various child processes kill -9 $PIDS # and they all need to DIE! (Killing the parent process would kill the child processes done # however there are actually two parent processes so this seems like a good idea?) echo -n "Starting my_app: " /usr/local/my_app/sbin/my_app_bin & exit $? ;; <snip snap snorum> Any input is greatly appreciated! Regards, James ;) -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GIT/MU/U dpu s: a--> C++>$ U+> L++> B-> P+> E?> W+++>$ N K W++ O M++>$ V- PS+++ PE++ Y+ PGP t 5 X+ R- tv+ b+> DI D+++ G+ e(+++++) h--(++) r++ z++ ------END GEEK CODE BLOCK------ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20090603/d60df8e9/attachment-0001.html>
On Wed, Jun 03, 2009 at 10:29:08AM +0100, James Bensley wrote:> > restart) > echo -n "Stopping my_app: " > pgrep my_app | while read PIDS; do # I have chosen this method because > my_app spawns various child processes > kill -9 $PIDS # and they all need to DIE! > (Killing the parent process would kill the child processes > done # however there are > actually two parent processes so this seems like a good idea?) > echo -n "Starting my_app: " > /usr/local/my_app/sbin/my_app_bin & > exit $? > ;;I'd think that the pgrep is matching both the processes you want to kill _and_ the init.d script itself. Try "pgrep -x" which will exactly match the specified command. Even better, use "pkill -x -9 my_app" which will combine the grep and kill tasks in a single command; the -9 specifies the signal to send to the matches processes. "man pgrep" for more information on both commands. John -- "I'm sorry but our engineers do not have phones." As stated by a Network Solutions Customer Service representative when asked to be put through to an engineer. "My other computer is your windows box." Ralf Hildebrandt <sxem> trying to play sturgeon while it's under attack is apparently not fun. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: <http://lists.centos.org/pipermail/centos/attachments/20090603/0ab21e2c/attachment-0001.sig>
On Wed, 2009-06-03 at 10:29 +0100, James Bensley wrote:> Hey Listees, > > I have a question (hopefully quite simple) about the system V init > scripts in /etc/init.d. I have an app installed and for some reason it > had no system V init script. It has been installed a long while now so > I can't quite remember whats going on but basically I remember I wrote > the init script my self and the app is enabled as a service which > chkconfig to run at system levels 3, 4 and 5. However when I wrote the > script I only ever added a "start" and "stop" clause to it (why I > can't remember?), now I need to add a "restart" clause but I'm having > an issue. > > I have pasted the code below from my "restart" clause but from what I > can tell, it is killing the app but not restarting it, I think because > the killing process is still in action so when it start the app again > it just gets killed straight away. If I enter "/etc/init.d/my_app > restart" the app is terminated but does not start again, entering > "/etc/init.d/my_app start" immediately after fires it up straight away > so there is no problem there; can someone suggest a better way as my > scripting is very begginnerish (if thats a word) and I don't doubt for > a second I am doing this in a very inefficient manner: > > <snipety snip snip> > > restart) > echo -n "Stopping my_app: " > pgrep my_app | while read PIDS; do # I have chosen this method because > my_app spawns various child processes > kill -9 $PIDS # and they all need to > DIE! (Killing the parent process would kill the child processes > done # however there are > actually two parent processes so this seems like a good idea?) > echo -n "Starting my_app: " > /usr/local/my_app/sbin/my_app_bin & > exit $? > ;; > > <snip snap snorum>--- Well from what I understand there is quit a debate on processes and forking them. One is released from the shell when it is forked into the background to release the process from the shell. This also seems to be the method for daemons under linux/unix. At least that's what I'm told. ############### def createDaemon(): try: #fork so parent can exit if #first child try: #fork second child and exit zombies if #second child else: os.exit ############### JohnStanley
From: James Bensley <jwbensley at gmail.com>> I have pasted the code below from my "restart" clause but from what I can > tell, it is killing the app but not restarting it, I think because the > killing process is still in action so when it start the app again it just > gets killed straight away. If I enter "/etc/init.d/my_app restart" the app > is terminated but does not start again, entering "/etc/init.d/my_app start" > immediately after fires it up straight away so there is no problem there;You kill all "*my_app*" processes and your init script is called... "my_app_..."? ^_^ Also, it is easier to create functions for start, stop, etc... Then restart = start; stop Have a look at other init scripts. JD