Hi I'm working on a script to manage some stuff on my server and I'm trying to figure out how I can close my script when I'm done with it. I open it using &, but how can I kill it with another script? I'm trying to do this programmatically but I'm very unfamiliar with Linux.

  • Propik, please add a snippit from your script (or just a summary) so other users can help you detect what the problem could be. This way it's not really a question, just a guess – Ivo Flipse Jun 30 '10 at 18:12

I'm not really sure what you're asking. A properly written script should end on its own when it is done executing.

  • 5
    This should have been a comment – Ivo Flipse Jun 25 '10 at 6:31
  • @Ivo - I disagree, the answer is "A properly written script should end on its own when it is done executing." – MDMarra Jun 25 '10 at 13:45
  • if so than this isn't a real question, because it's obviously flawed. Furthermore, this answer doesn't help propik solve his problem one bit, even though you're right – Ivo Flipse Jun 26 '10 at 14:00
  • @Ivo - I agree, but without him editing his question to provide the script there is no way for me to tell him why his end-condition isn't being met. Any other solution is just a band-aid. – MDMarra Jun 30 '10 at 15:41

If I understand correctly you have a script that you run in the background using & and you would like to kill it after a while. Also hopefully this script is supposed to be running for a while.

You can find the process id of you script by using a command such as:

ps  aux | grep test.sh| awk '{print $2}'

replace test.sh by the name of your script

to kill it run

kill `ps  aux | grep test.sh| awk '{print $2}'`

ps is the command to list the processes on the computer, aux says print every process on the system, grep search for a string from standard input (the output of ps in this case), awk is a program that allows you to process some text and run stuff on it. In this case I asked it to print the second column of the ouput of the first two commands.

  • 2
    This would also kill other processes that had the grepped string such as vi test.sh. Unless you do grep -v grep or grep [t]est.sh, the PID of the grep command will be included. Take a look at pkill. It lets you restrict selected processes by username and other criteria and only looks at the command name and not its arguments (unless you tell it to). – Dennis Williamson Jun 25 '10 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.