Daemonizing a Process in Linux

Most of the times when we need to run some Java, Node.js or python program in background, so it could stay running even after you leave console all we do is put “&” in the end of the command.

$java -jar SimpleService.jar &

The problem here is that when you leave the bash shell your process will become “Orphan” and it’s up to the OS if INIT should adopt the process or kill the process.

To fix this we can force the process to be detached from your shell and become child process of INIT by putting nohup in front of command.

$nohup java -jar SimpleService.jar &

It’s all good for testing purposes, but services are supposed to run as “daemons” under some service supervisor. You can find many service supervisors with which you manage your services on Linux and we will cover few of the defaults that come with Linux.

Why do you need it ?

a. auto start/stop the service on startup/shutdownb. controlling which service should start firstc. controlling which service should stop first is equally importantd. INIT controlled processes directly which provides more options like respawn, post-start, etc..