LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2008, 11:51 AM   #1
Azk00
LQ Newbie
 
Registered: Jan 2008
Location: Madrid, Spain
Distribution: OpenSuse
Posts: 2

Rep: Reputation: 0
How to stop a process running in background (on a shell script)


Hi, this is my first approach to shell scripts and I’m a bit lost .

I have a server-client app, and I want to run a server with a pair of clients. So I run this 3 processes in background, but when I need to stop them, I don’t know how. Is there any equivalent to Ctrl-C with shell scripts?

I’ve been searching on bash man for something like getting its pid and later killing them, but couldn’t find how. Any suggestion?

And another silly question is How can I make my shell wait for N seconds before running next command?
I need the second client to start about 20 secs after the 1st one has connected so I’ve solved this by repeating a few “ls” between them but its ridiculous and I’m sure there has to be another way.

To give you an idea of how newbie I am, here’s my script:

Code:
#!/bin/bash
set –x

cd server
./run > servertrc.txt &

ls
ls
[..]
ls

cd ../client1
./run &

ls
ls
[..]
ls

cd ../client2
./run &

exit 0
Thanks for any help or hint!
 
Old 01-31-2008, 12:14 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,346

Rep: Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552
Quote:
Originally Posted by Azk00 View Post

I’ve been searching on bash man for something like getting its pid and later killing them, but couldn’t find how. Any suggestion?
You can find the PIDs of all running processes with the top command. See:

man top


Quote:
Originally Posted by Azk00 View Post

I need the second client to start about 20 secs after the 1st one has connected so I’ve solved this by repeating a few “ls” between them but its ridiculous and I’m sure there has to be another way.
Use the sleep command. See:

man sleep

----------------------
Steve Stites
 
Old 01-31-2008, 12:35 PM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
You can find the pid with pgrep. Kill the running program or script with kill. Or combine the two:

kill `pgrep <programname>`
 
Old 02-01-2008, 04:24 AM   #4
Azk00
LQ Newbie
 
Registered: Jan 2008
Location: Madrid, Spain
Distribution: OpenSuse
Posts: 2

Original Poster
Rep: Reputation: 0
Done, thanks to Steve Stites and dive for spending their time on answering .
I finally stopped the process with
Code:
pkill -x <programname>
and seems to work ok.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script switching it self into background process knockout_artist Linux - Software 3 11-24-2007 11:33 AM
Stop process while running script azazel11998 Programming 1 07-01-2007 04:12 AM
running process in background forever danran Linux - General 2 11-19-2006 09:44 AM
Running kismet as a background process coolnicklas Linux - Software 2 01-04-2006 03:33 PM
background running process elbriga Programming 2 09-21-2004 06:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration