11

I run a server executable in Centos using the following command "nohup server &". Now I need to kill the process "server". But I tried "ps -a" command to get the PID but I couldnt get the process. Now how to kill the "server" now?

2vision2
  • 4,933
  • 16
  • 83
  • 164
  • 2
    ps auxwww|grep -i 'server' should return all process which has 'server' in them. Otherwise, server may have already stopped. – Usman Saleem Jan 04 '13 at 06:04
  • @Usman saleem thanks this solves my answer. I have One more doubt "nohup server &" is the right command, to run a process backgroung even if i close the terminal? – 2vision2 Jan 04 '13 at 06:07
  • @Mikhail thanks this solves my answer. I have One more doubt "nohup server &" is the right command, to run a process background even if i close the terminal? – 2vision2 Jan 04 '13 at 06:08
  • 1
    Yes it is, you can determine the PID right when you invoke the command: 'nohup server &' followed by 'print $! >> server_pid_file' – Usman Saleem Jan 04 '13 at 06:10
  • 1
    yes, using nohup command means that your 'server' process will discard 'HUP' interrupt which OS sends when you close the terminal. It will also discard 'kill -HUP pid' or 'kill -1 pid'. – Usman Saleem Jan 04 '13 at 06:16
  • You might also want to use `batch` or `at` to run your `server` – Basile Starynkevitch Jan 04 '13 at 06:24

4 Answers4

20

ps auxwww|grep -i 'server' should return all process which has server in them. Otherwise, server may have already stopped.

You should be able to determine the PID (and store it in a file) as follows:

nohup server &
print $! >> my_server.pid
Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
Usman Saleem
  • 1,665
  • 9
  • 18
15

If a nohup.out file was created, you may run in bash:

# lsof nohup.out

it will return the pid which created/is running the file

best regards!

ignivs
  • 636
  • 7
  • 18
5

There is no definitive way to catch the exact process with the help of ps command, but you can use the following:

ps -a | grep "server"

You will get a list of all the processes running with the name "server"

Or, you can use any other keywords as well to grep the ps output.

Arpit
  • 6,212
  • 8
  • 38
  • 69
1

The best way to launch a server in centos is with the service command.

So service httpd start

There is a chance that you want to write your program as a daemon

A daemon (or service) is a background process that is designed to run autonomously,with little or not user intervention. The Apache web server http daemon (httpd) is one such example of a daemon. It waits in the background listening on specific ports, and serves up pages or processes scripts, based on the type of request.

See http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

Mikhail
  • 7,749
  • 11
  • 62
  • 136
  • I just need to run my executable background? can you give some example? – 2vision2 Jan 04 '13 at 06:10
  • @2vision2 you want to write your program like a linux daemon see http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html – Mikhail Jan 04 '13 at 07:00