Jump to content

shell script that kills another running script?


Guest chotie
 Share

Recommended Posts

Guest chotie

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...