Guest chotie Posted August 22, 2003 Report Share Posted August 22, 2003 Hello! I am trying to find out how to kill a running script with a script. I have tried a little myself so fat but it doesnt work as it should. ================================== var = ps ax|grep anotherscript.sh # this line will get the process number of "anotherscript.sh" kill var # here i will kill the process "anotherscript.sh" ================================== However - this does not work and i am not sure why. Am i doing anything wrong? Thanks for listening. Br, Christian O. chot@home.se Quote Link to comment Share on other sites More sharing options...
MottS Posted August 22, 2003 Report Share Posted August 22, 2003 Here is my test. It works with xmms. Just replace xmms by the process name of your script. #!/bin/bash var="$(eval pidof xmms)" if [ "$var" = "" ] then echo "XMMS is not running" else echo "XMMS is running. I'm gonna kill it;-)" kill $var fi HTH MOttS Quote Link to comment Share on other sites More sharing options...
Guest chotie Posted August 22, 2003 Report Share Posted August 22, 2003 I will use this. My only trouble would now be how i will be able to run this over ssh. Thanks again!! :) Br, Christian O. chot@home.se Quote Link to comment Share on other sites More sharing options...
MottS Posted August 22, 2003 Report Share Posted August 22, 2003 Why not put that script on the remote machine (the one you want to ssh to) and execute it every X seconds/minutes/hours/days in a cron job? OR put the script on the remote machine (with sftp or ftp) and login to this machine with ssh to execute it once in a while.. What do you wanna do exactly? MOtTS Quote Link to comment Share on other sites More sharing options...
aru Posted August 24, 2003 Report Share Posted August 24, 2003 Just a couple of suggestions to add to Motts' posts: http://www.mandrakeusers.org/viewtopic.php...p?p=33870#33870 The question posted in thread which I've linked above is very similar to yours... well, not exactly, but you'll get the idea: killall anotherscript.sh should do the trick ;) To answer directly to your question, the problem is the syntax, you'll need to learn a bit more the basics of variable assignments (remember that bash is very strict on it because it is not only a programming language but it is also a command interpreter, so spaces have meanigs) [side note] You don't need to give .sh extensions to your shell scripts, that is a bad habit that should be avoided. Even more if, lets say, your shell scripts becomes more and more important for your system mainteinance. For example, foo.sh is a shell script you wrote to perform some task, then that script becomes critical for your daily usage and hence is called from cron, from a bunch of scripts, and/or from command line ver often. One day you reaized that that script has to be updated and you think that python, perl or maybe C will perform better the task the script does than bash: What will you do? rename it as foo, foo.py, or foo.pl and then search for every place from where it is called to update/fix the name? It is better to don't use arbitrary extensions. Just an example: /usr/bin/run-parts in Slackware is a shell script, while in mandrake is a C written program and both perform exactly the same task, so a user from any of both distros can call it w/o taking care of how or which language was used to write it. Quote Link to comment Share on other sites More sharing options...
Guest chotie Posted August 26, 2003 Report Share Posted August 26, 2003 Thank you everybody! I have got it working now. (I am running a script on the serverside). Br, Christian O. chot@home.se Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.