Kill a program from bash

Tags
bash;, beginners

 

 
Thread Tools Search this Thread
# 1  
Old 07-25-2014
Kill a program from bash

to kill a program in bash, for instance 'mousepad' I use
Code:
kill $(pidof mousepad);

or
Code:
pkill mousepad

But it only works if we use another bash window;

If it is started from the same bash, that does not work:
Code:
#mousepad;kill $(pidof mousepad);

In this case, it looks like mousepad hangs and the second command (kill $(pidof mousepad) cannot be executed.

So if I need to write a bash program which starts a program, run some routines and kill that program how do I do?

Thanks a lot guys!
# 2  
Old 07-25-2014
You could try running mousepad in the background:

Code:
mousepad &

# 3  
Old 07-25-2014
thanx in2nix4life
I have already tried that:
Code:
mousepad&;pkill mousepad
bash: syntax error near unexpected token `;'

# 4  
Old 07-25-2014
So you're trying to start mousepad and then immediately kill it? Please provide more detail on what you're trying to accomplish.
# 5  
Old 07-25-2014
You have a child process. If you check the pid of your current process
Code:
echo $$

BEFORE the command, then check
Code:
ps -ef | grep mousepad

AFTER the
Code:
mousepad &

command to get the pid of the child process then you can see what is happening.

Do you know about job commands in bash and how kill works on them?
fg and bg and jobs are your friends here. Plus, mousepad may run setuid as an independent daemon - I know nothing about it. Which means it may run as root and you cannot kill it.

Lose mousepad, and try something simple like
Code:
sleep 10 &

for testing.
# 6  
Old 07-25-2014
I found what was the problem;

Running:

Code:
/usr/bin/mousepad &sleep 3;pkill mousepad

works. I put 'sleep 3' for testing purpose; we can put whatever command we want.

thanx guys!

---------- Post updated at 02:11 PM ---------- Previous update was at 02:09 PM ----------

Quote:
Originally Posted by arpagon
I found what was the problem;

Running:

Code:
/usr/bin/mousepad &sleep 3;pkill mousepad

or
Code:
mousepad &sleep 3;pkill mousepad

work. I put 'sleep 3' for testing purpose; we can put whatever command we want.

thanx guys!
# 7  
Old 07-25-2014
If you started the background process from your script, you can kill it using the job number.
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Bash Scripting program rami-ali Homework & Coursework Questions 2 11-21-2018 11:57 AM
Kill specific terminal using shell/bash kamil-mech Shell Programming and Scripting 0 03-05-2015 01:36 PM
Question regarding Bash program biochemist Programming 11 10-06-2013 06:33 PM
Bash script show Kill system output sachinbutala Shell Programming and Scripting 2 02-02-2011 02:24 AM
Functions, exit, and kill in bash brsett Shell Programming and Scripting 1 06-11-2010 12:17 PM
Kill a running program? petel1 UNIX for Dummies Questions & Answers 3 05-26-2010 08:05 AM
Program Bash VERY URGENT danihj Shell Programming and Scripting 1 02-13-2010 10:19 AM
PROGRAM BASH danihj Shell Programming and Scripting 1 02-13-2010 10:11 AM
Kill shell script when host program not running/disk unmounted pcwiz Shell Programming and Scripting 2 10-02-2009 11:04 AM
Kill -9 within Bash script kicks out usage info mglenney Shell Programming and Scripting 1 07-09-2009 04:35 PM
Kill the running program akhtar.bhat Shell Programming and Scripting 1 12-26-2008 07:48 AM
Trying to kill a program via help command. TiznaraN Shell Programming and Scripting 5 04-22-2008 01:57 PM
kill multiple instances of the same program ipzig Shell Programming and Scripting 12 12-17-2007 02:42 AM
kill textrdit program jeter Programming 6 06-07-2006 10:12 AM
C program to kill root processes TioTony Programming 8 07-25-2003 12:16 AM