Friday, October 1, 2010

Starting daemon process using screen

Here is a good way to start a daemon background process using screen.
screen -d -m -L command args...
This tells screen to create a new session (-m), log its output (-L) and detach immediately (-d), which the command args... is run. The log is written to screenlog.n in the current directory, where n is the "window" number inside the screen session. The logging is buffered and flushed periodically, and the log file can be logrotated.

The screen session supports process control, i.e. stopping a running daemon. To do that, the screen session must be started with a session name (-S sessionname), and it can be killed later with the name.
screen -S pingsession -d -m -L ping localhost
sleep 20
screen -r pingsession -X quit
The advantage is that the daemon process does not have to do anything to turn itself into a daemon or to support process control. A list of screened daemons can be shown using screen -list command.

1 comment:

amit.codename13 said...

With some awk we can make screen name a unique identifier for the daemon process
http://blog.amitkarmakar.in/2013/11/use-screen-to-run-script-as-daemon.html