Friday, January 13, 2012

Background process management

I ran an impromptu tutorial the other day on background process management from the bash command line with one of my colleagues. I thought it might be helpful to write it up for anyone else who might find it useful.

We were starting a redis server in this case, bit this works for any UNIX process.

So, we can start the server in the background by suffixing the command with an &

$ redis-server /usr/local/etc/redis.conf &

We can then list background processes by running

$ jobs
[1]+ Running redis-server /usr/local/etc/redis.conf &


The [1] indicates a job "handle" (I'm sure there is a proper name for it, but I don't know what it is), that we can pass to the following commands to manage the process:


- fg %1: pulls process 1 into the foreground
- bg %1: pushes process 1 into the background
- kill %1: kills process 1



If you start another process, it will be assigned the next available id which is %2 in this case

$ tail -f project.clj &
[2] 37913
$ (defproject show-grid "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.2.1"]])
$ jobs
[1]- Running redis-server /usr/local/etc/redis.conf &
[2]+ Running tail -f project.clj &


Killing a process will free up it's handle and it will be reused. This means that for most casual uses, you will only need %1 and, occasionally, %2 unless you run a lot of deamons for some reason.

$ kill %2
[2]+ Terminated: 15 tail -f project.clj
$ jobs
[1]+ Running tail -f project.clj &
$ tail -f README &
[2] 37927

## Usage
FIXME: write
## License
Copyright (C) 2011 FIXME
Distributed under the Eclipse Public License, the same as Clojure.
$ jobs
[1]- Running tail -f project.clj &
[2]+ Running tail -f README &


The problem with simply putting things into the background is that the standard output stream for the process remains attached to your terminal and it will continue to write to your session while you are trying to work on other things. Usually, you want to run the process silently and log the output to a file using nohup like this. Notice that we have merged the stdout and stderr streams so nothing gets lost.

$ nohup redis-server /usr/local/etc/redis.conf > ./redis.log 2>&1 &
[1] 21212
$ tail -f redis.log
[21212] 10 Jan 11:16:39 * Server started, Redis version 2.4.2
[21212] 10 Jan 11:16:39 * DB loaded from disk: 0 seconds
[21212] 10 Jan 11:16:39 * The server is now ready to accept connections on port 6379
[21212] 10 Jan 11:16:39 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:16:44 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:16:49 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:16:54 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:16:59 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:17:04 - 0 clients connected (0 slaves), 922288 bytes in use
[21212] 10 Jan 11:17:09 - 0 clients connected (0 slaves), 922288 bytes in use

No comments:

Post a Comment