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------
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
On Wed, Jun 03, 2009 at 04:42:09AM -0500, John R. Dennison wrote:
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.
Blame it on the early hour but it should probably actually read:
"pkill -x -9 my_app_bin"
John
As you spotted john my killing loop was killing the system V script itself also hence it never starting my_app again, so instead a simple if skips the process ID of the system V script itself so it can continue on to start my_app again:
I'd think that the pgrep is matching both the processes you want to kill _and_ the init.d script itself.
My script looks like the following:
restart) echo "Stopping my_app:" pgrep my_app | while read PIDS do if [ "$PIDS" -eq $$ ]; then continue fi kill -9 $PIDS done echo "Starting my_app:" /etc/init.d/my_app start & exit $? ;;
Thanks for the help everyone its 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------
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@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