How to run a script as a background process?
We all agree that at times we build stuff as a side project to test out things and eventually scrap them before bringing it into picture in a larger project. More so, we want to minimize the resource consumption on this side adventure and yet achieve the maximum closeness to the environment it is supposed to be used in.
One such scenario could be a script which is supposed to run as a background process on your local system or server. The script might be intended to be a daemon or a web service. While you might have proper environment set up to handle the dependencies for this script in production, nobody wants to go through setting up and configuring an environment to test out one single script in isolation.
Ever wondered how can we make that script that you just created run as a background process? Your reasons for making such a script may vary but if you are looking for a simple way to get out of this situation, this is for you.
To achieve this in a Debian system we are going to use Systemd (Check whether your OS is based on Debian). Systemd is the default system and service manager post Debian Jessie and should be available out of the box (reference). If not, you can go through the installation steps.
Alternatively to check whether your default init is systemd, you can use this handy script here
# sudo ps -p 1 -o comm=systemd
Lets dive in and create the background process. I’m assuming that the script that you wish to run in background is already handling the logic to iterate itself without exiting (if not the process will end with your script). Also, this example is built using python so for other languages you’ll have to fill in the missing pieces yourself.
The first thing you want to know is where the systemd processes reside. You can find them by going to :
# cd /etc/systemd/system/
Lets create our own service file (I have named it bg_process) and add some code to it:
# sudo touch bg_process.service
# sudo vim bg_process.service
The only parameter required to be changed to make the above script functional for you is ExecStart. In general, your ExecStart will look something like this:
If you are using a virtual environment, simply replace the default python path with the path of your environment.
The only thing left to do is to let the system know that we have a new service to run and start it. Moving forward your script will be initialized on every system boot.
# sudo systemctl daemon-reload
# sudo service bg_process start
And voila your script is now a background service. To check for it’s status you can anytime run:
# sudo service bg_process status
A simple google search or browsing on stackoverfow can land you on a variety of solutions to achieve this. If this is something that is essential to your project, you might want to seriously consider all the options before settling on the correct solution.
Please feel free to add in your comments and suggestions.